my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes On finalize... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "On finalize..." Watch "On finalize..." New topic

On finalize...

Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Interesting. A prossible use of finalize.
It seems that finalize is 'suggested to be' removed though: Link
From Thinking in Java, 3rd ed. Revision 4.0

So it appears that finalize( ) is only useful for obscure memory cleanup that most programmers will never use. However, there is a very interesting use of finalize( ) that does not rely on it being called every time. This is the verification of the termination condition[23] of an object.
At the point that you�re no longer interested in an object�when it�s ready to be cleaned up�that object should be in a state whereby its memory can be safely released. For example, if the object represents an open file, that file should be closed by the programmer before the object is garbage collected. If any portions of the object are not properly cleaned up, then you have a bug in your program that could be very difficult to find. The value of finalize( ) is that it can be used to eventually discover this condition, even if it isn�t always called. If one of the finalizations happens to reveal the bug, then you discover the problem, which is all you really care about.

[ January 16, 2004: Message edited by: Jim Crawford ]

<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

interesting little tidbit of info.

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
That's old news. Class finalization was specified in the first edition of the JLS in 12.7, Finalization of Classes. The class finalization feature does not appear in the second edition.
The instance finalization feature that remains is not the same as the obsolete class finalization feature. Instance finalization occurs before instances are garbage collected. Class finalization was intended to occur before classes were unloaded.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
I agree. Here's the link:
subject: On finalize...
It's not a secret anymore!