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 Beginning Java and the fly likes Do we need to call finalize() method explicitly on an object? 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 » Java » Beginning Java
Bookmark "Do we need to call finalize() method explicitly on an object?" Watch "Do we need to call finalize() method explicitly on an object?" New topic
Author

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

Jalli Venkat
Greenhorn

Joined: Aug 10, 2006
Posts: 27
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.


venkat
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
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.


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sandeep Kumar Jakkaraju
Ranch Hand

Joined: Feb 25, 2009
Posts: 75
I guess we can override the defualt finalize() method !!! .......... may be sometimes useful..to make the members null referenced !!
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

You might want to read this : Effective Java: Item 6 Avoid Finalizers


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Sandeep Kumar Jakkaraju
Ranch Hand

Joined: Feb 25, 2009
Posts: 75
Thanks ... I didn't load this book onto my classpath yet ..... so just thought finalize might me useful .....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do we need to call finalize() method explicitly on an object?
 
Similar Threads
finalize method
Garbage Collection Q
garbage collection question
garbage collection's doubt
Garbage collection??