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 goes
}
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3