Finalizer Chaining
Home ] Up ] An Example ] Points to Notice ] Constructor Chaining ] Superclass Method Calls ] [ Finalizer Chaining ]

 

 

You might think that, because Java enforces constructor chaining, that it would enforce finalizer chaining -- in other words, that a finalizer method for a subclass would automatically invoke the finalizer method for the superclass.

Java does not do this. You must do this explicitly, if you write a subclass finalizer method, and if you want this behavior.

Usually, the code will look as follows:

protected void finalize() throws Throwable
{
    /* ... */
    super.finalize();	// Typically the last line...
}
Again, do not think of a finalize method being equivalent to a destructor (as in C++), or something that can be used as some kind of Java "deconstructor".  In most cases, if you think along those lines, you will get into trouble!

 

This page was last modified on 02 October, 2007