EXECUTION
Finalization of Class Instances
12.6
class Test extends Super {
int indiana = (int)Math.PI;
//
That is,
3
public static void main(String[] args) {
Test t = new Test();
t.printThree();
}
void printThree() { System.out.println(indiana); }
}
produces the output:
0
3
This shows that the invocation of
printThree
in the constructor for class
Super
does not invoke the definition of
printThree
in class
Super
, but rather invokes
the overriding definition of
printThree
in class
Test
. This method therefore
runs before the field initializers of
Test
have been executed, which is why the first
value output is
0
, the default value to which the field
three
of
Test
is initialized.
The later invocation of
printThree
in method
main
invokes the same definition
of
printThree
, but by that point the initializer for instance variable
three
has
been executed, and so the value
3
is printed.
See 8.6 for more details on constructor declarations.
12.6 Finalization of Class Instances
The class
Object
has a
protected
method called
finalize
( 20.1.11); this
method can be overridden by other classes. The particular definition of
finalize
that can be invoked for an object is called the
finalizer
of that object. Before the
storage for an object is reclaimed by the garbage collector, the Java Virtual
Machine will invoke the finalizer of that object.
Finalizers provide a chance to free up resources (such as file descriptors or
operating system graphics contexts) that cannot be freed automatically by an auto
matic storage manager. In such situations, simply reclaiming the memory used by
an object would not guarantee that the resources it held would be reclaimed.
The Java language does not specify how soon a finalizer will be invoked,
except to say that it will happen before the storage for the object is reused. Also,
the Java language does not specify which thread will invoke the finalizer for any
given object. If an uncaught exception is thrown during the finalization, the excep
tion is ignored and finalization of that object terminates.
231
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved