This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Do we need to call finalize() method explicitly on an object?

 
Jalli Venkat
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Is it necessary to call finalize() method on an abject explicitly or it will be called by garbage Collector automatically whenever the object eligible for Garbage Collection..?

Please answer this.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any search to google will tell you that its called implicitly by the GC when it determines that the object cannot be reached. I believe putting anything much in it is discouraged, even to the point of never using it.
 
Ankit Garg
Sheriff
Posts: 9597
36
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Tom said, it is called automatically by the GC. But you can still call it explicitly but that is discouraged and that will not destroy the object from the heap. Also the finalize method has been declared protected in the Object class so that you can make it inaccessible from outside your class and its sub classes. This is done solely to restrict any code outside your class (and its sub classes) from calling the finalize method...
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess we can override the defualt finalize() method !!! .......... may be sometimes useful..to make the members null referenced !!
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to read this : Effective Java: Item 6 Avoid Finalizers
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ... I didn't load this book onto my classpath yet ..... so just thought finalize might me useful .....
 
Rob Spoor
Sheriff
Posts: 20827
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep Kumar Jakkaraju wrote:I guess we can override the defualt finalize() method !!! .......... may be sometimes useful..to make the members null referenced !!

If you do, always call super.finalize():

I usually only add finalizers to make sure that resources are released, but I always provide means to release resources explicitly. Like Monu's link says, finalizers should only be used as a safety net of sorts; never rely on it, only use it "just in case".
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!