Before an object is garbage collected, the runtime system calls its
finalize()
method. The intent is for finalize()
to release system resources such as open files or open sockets before getting collected. Your class can provide for its finalization simply by adding a method in your class named
finalize()
. Your finalize()
method must be declared as follows: protected void finalize () throws throwable
The
finalize()
method is declared in the java.lang.Object class. Thus when you write a finalize()
method for your class you are overriding the one in your superclass. If your class's superclass has a
finalize()
method, then your class's finalize()
method should probably call the superclass's finalize()
method after it has performed any of its clean up duties. This cleans up any resources the object may have unknowingly obtained through methods inherited from the superclass. protected void finalize() throws Throwable {
. . .
// clean up code for this class here
. . .
super.finalize();
}
No comments:
Post a Comment