• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

On finalize...

 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interesting little tidbit of info.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic