The finalize() method
The finalize() method can be defined to be called before an object is destroyed by the garbage collector.
finalize() ensures that an object is completely eliminated, like closing an open file.
To add a finalizer, define the finalize() method in the class.
The Java Runtime calls finalize() before recycling an object of the class.
General form of the method:
protected void finalize( )
{
// part where the completion code enters
}
The protected keyword limits access to finalize().
finalize() is called before garbage collection, not when an object goes out of scope.
It is not guaranteed when, or if, finalize() will be executed.
If the program terminates before garbage collection, finalize() will not be executed.
finalize() should be used as a fallback procedure for appropriate handling of special resources or applications.
finalize() is a specialized method and is rarely needed in most programs.
The above is the detailed content of The finalize() method. For more information, please follow other related articles on the PHP Chinese website!