finalize : Java Glossary
home F words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-11-07 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
finalize
Note the z American spelling is finalize. Java has all sorts of mechanisms for running special initialisation code, but it is rather weak on dealing with finalisation code. You can add a method called finalize to any class.
protected void finalize() throws Throwable {...}
When a object is about to be garbage collected, its finalize method will be run. The catch is, at shutdown, by default the finalizers are not run on all unreachable objects, so you can’t count on them to do things like close files.

// Force finalize methods to be run on exit
// Without this, unreachable objects may not
// have had finalize run when you quit.
System.runFinalizersOnExit( true );
Under no circumstances are finalizers run on reachable objects, so you can’t use them to close files for example.

You can also register a hunk of code to be run like this:

Runtime.addShutdownHook( Thread hook );
Unfortunately, addShutdownHook takes a memory-hogging Thread rather than a lightweight Runnable.

Some feel finalize should be deprecated, and you should use phantom references instead since they give much better performance. Finalizers interfere with garbage collection. Their main use is debugging. Use them to issue an error message is an object is garbage collected without its close(), dispose(), disconnect()… method being called.


CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 25,389. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/finalize.html J:\mindprod\jgloss\finalize.html