This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why Donot finalize method run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why Donot finalize method run?" Watch "Why Donot finalize method run?" New topic
Author

Why Donot finalize method run?

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643


In above code the finalize method donot run
i am not able to understand why.

Thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You have implemented finalize in your Test class. You do not create an instance of your Test class. So no instance of the Test class needs to be garbage collected (or rather, is eligible for garbage collection)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
But there are Integer objects inside for loop which should be garbage collected.
Awais Sheikh
Ranch Hand

Joined: Jun 07, 2006
Posts: 48
Hello,
Finalize method is called when object of a class is created and completes its work, but remember finalize method should in the class whose garbage it has to be collect.
In your code you implemented in the test class, but no where object of Test class is created so this finalize method will not called by gc. new Interger is calling finalize method of Integer class not Test class. Try the code below hope it will clear you.



Thanks
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Thanks Awais Sheikh
Now i understood it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why Donot finalize method run?
 
Similar Threads
AutoBoxing
Sun's mock question 4.
overriding question
Thread/ Exception
Garbage collection output doubt