In Java, the finalize()
method is a method provided by the Object
class that gets called by the garbage collector before an object is reclaimed (destroyed) by the garbage collector. It’s a part of Java’s garbage collection mechanism.
Here’s a brief explanation of the finalize()
method:
- Declaration in the
Object
Class:
- The
finalize()
method is declared in theObject
class as follows:protected void finalize() throws Throwable { // code to be executed before the object is garbage collected }
- It’s declared as
protected
, meaning that it is accessible within the same package and by subclasses.
- Automatic Invocation:
- The
finalize()
method is automatically invoked by the garbage collector when it determines that there are no more references to the object. - This usually happens when the object is no longer reachable from any live threads or when it goes out of scope.
- Override for Cleanup:
- In practice, it’s not common to use the
finalize()
method for general cleanup tasks, as it has some limitations and uncertainties. - Resources such as file handles or network connections should be explicitly released using
try-with-resources
orfinally
blocks to ensure proper resource management.
- Limitations and Best Practices:
- The
finalize()
method is called by the garbage collector, and there is no guarantee on when it will be executed. - Objects waiting to be garbage collected consume memory until they are actually collected, which might be problematic for resource-intensive applications.
- It’s generally recommended to use other mechanisms, such as
try-with-resources
for resource cleanup and theAutoCloseable
interface, instead of relying on thefinalize()
method.
Here’s a simple example to illustrate the use of finalize()
:
class MyResource {
// Some resource-specific code
@Override
protected void finalize() throws Throwable {
try {
// Cleanup code for the resource
System.out.println("Finalizing MyResource");
} finally {
// Call the finalize() method of the superclass
super.finalize();
}
}
}
public class FinalizeExample {
public static void main(String[] args) {
MyResource resource = new MyResource();
// Set the reference to null to make the object eligible for garbage collection
resource = null;
// Suggest the garbage collector to run, but it doesn't guarantee immediate execution
System.gc();
}
}
In this example, the finalize()
method of MyResource
is called when the garbage collector reclaims the object. However, relying on finalize()
for critical cleanup tasks is not considered a best practice in modern Java programming.
Sometimes an object will need to perform some action when it is destroyed.
For example, if an object is holding some non-Java resource such as a file handle or character font, then you might want to make sure these resources are freed before an object is destroyed.
To handle such situations, Java provides a mechanism called finalization. By using finalization, we can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector
To add a finalizer to a class, you simply define the finalize( ) method. The Java run time calls that method whenever it is about to recycle an object of that class.
Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state or indirectly through other referenced objects
The finalize( ) method has this general form:
protected void finalize( )
{
// finalization code here
}
the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class