|
Rationale for Eliminating Class Finalization from the Java Programming
Language
Briefly, class finalization was
-
redundant
-
rarely necessary
-
subtle to use
-
could be removed without compatibility problems
These points are discussed in more detail below.
Class Finalization is redundant
The functionality of class finalization can be subsumed by instance finalization.
Any static state of the class can be encapsulated in an instance of some
class, which can have the appropriate finalizer. That is, instead of
class Foo{
static SomeResourceType x = someResource;
public void classFinalize(){ ... clean up someResource ...}
}
write
class SomeResourceManager{
SomeResourceType sr;
SomeResourceManager(SomeResourceType y){sr = y;}
public void finalize(){ ... clean up someResource ...}
}
class Foo{
static SomeResourceManager x = new SomeResourceManager(someResource);
}
Since we're talking about a static variable, the overhead of some wrapper
object like SomeResourceManager is negligible. If the finalizer
needs to to in the scope of Foo, SomeResourceManager
can be a nested class.
The bottom line is that anything one can do with a class finalizer,
one can do with ordinary finalizers without much extra trouble. At the
same time, class finalization adds complexity to the definition and implementation
of the language.
Class Finalization is rarely necessary
Class finalization has not been implemented in the JDK (nor, to the
best of our knowledge, is it implemented by any other vendor). We
received almost no complaints about this, which lead us to believe that
the feature is not as useful as was once thought.
Class Finalization is subtle to use
Understanding the appropriate use of class finalization is very subtle.
Users may erroneously expect a guarantee that class finalization is invoked,
when in fact, there is no such guarantee. You never have a guarantee that
either class or object finalization will in fact take place. If you really
must free a resource, you need to track its uses yourself, regardless
of whether finalization is implemented in the system.
Class Finalization could be removed without compatibility problems
Eliminating this feature has no effect on existing programs, since it was
unimplemented.
|