A finalizer is a special method in an object-oriented language that is executed when an object is garbage collected.
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 finalizer 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.