A finalize() is a special method in an object-oriented language that is executed when an object is garbage collected.
A finalize(), or a finalizer, is a special method in an object-oriented language that is executed when an object is garbage collected.
Java Documentation (Javadoc) defines the moment when the finalize() is invoked as:
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
However, this may never happen in the life of a program if the object is always accessible. Due to the lack of programmer control over their execution, it is usually recommended to avoid finalizers for any but the most trivial operations.
Starting Java9. finalize()
method has been officially deprecated.
javadoc for Java9's java.lang.Object finalize()