This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes explicit finalize( )? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "explicit finalize( )?" Watch "explicit finalize( )?" New topic
Author

explicit finalize( )?

Sandra Baker
Greenhorn

Joined: Jul 10, 2002
Posts: 26
Hi, is it necessary to explicitly implement finalize() method in every class we defined? I thought it would be taken care of by JVM.... thanks
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Sandra,

Hi, is it necessary to explicitly implement finalize() method in every class we defined? I thought it would be taken care of by JVM....

Nope. The only time you need finalize is for any clean up that might be required when the GC scoops up the object. For example closing a file or database connection.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17271
    
    6

And to be totally honest, you should avoid using a finalize method at all costs. You have no guarantee of when the finalize method will be called. And having a finalize method allows for what is called reincarnating a dead object. Which leads to other problems.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
jQuery in Action, 3rd edition
 
subject: explicit finalize( )?
 
It's not a secret anymore!