In Java, the “finalize” method is a special method that is automatically called by the garbage collector before an object is garbage collected. It provides an opportunity for the object to perform any necessary cleanup operations before it is removed from memory.
In this blog post, we will explore the “finalize” method in Java, along with multiple examples. Lets start learning about finalize method in java.
What is the “finalize” Method?
The “finalize” method is a protected method defined in the Object class. Object class is the root of all Java classes. finalize has the following signature:
protected void finalize() throws Throwable
The “finalize” method is automatically invoked by the garbage collector when it determines that there are no more references to an object and it is eligible for garbage collection.
Example 1: Using the “finalize” Method
Let’s see an example that demonstrates the usage of the “finalize” method:
public class FinalizeExampleJava {
public static void main(String[] args) {
FinalizeExampleJava obj = new FinalizeExampleJava();
obj = null;
System.gc();
}
@Override
protected void finalize() throws Throwable {
System.out.println("Finalize method called");
super.finalize();
}
}
Output
Finalize method called
Explanation:
In this program, we create an instance of the `FinalizeExampleJava` class and assign it to the `obj` variable. We then make the object eligible for garbage collection by setting `obj` to `null`. Finally, we explicitly request garbage collection by calling `System.gc()`. When the garbage collector runs, it will invoke the `finalize` method of the `FinalizeExampleJava` class, allowing us to perform any necessary cleanup operations.
Example 2: Overriding “finalize” in a Subclass
Let’s consider an example where we override the “finalize” method in a subclass:
class FinalizeExampleJava {
@Override
protected void finalize() throws Throwable {
System.out.println("Finalize method called");
super.finalize();
}
}
public class FinalizeSubclassExample extends FinalizeExampleJava {
@Override
protected void finalize() throws Throwable {
System.out.println("Subclass finalize method called");
super.finalize();
}
public static void main(String[] args) {
FinalizeSubclassExample obj = new FinalizeSubclassExample();
obj = null;
System.gc();
}
}
Output
Subclass finalize method called
Finalize method called
Explanation:
In this example, we create a subclass called `FinalizeSubclassExample` that extends the `FinalizeExampleJava` class. We override the `finalize` method in the subclass to provide subclass-specific cleanup operations. The `super.finalize()` call ensures that the superclass’s `finalize` method is also invoked.
Conclusion:
In this blog post, we have leaned about the “finalize” method in Java. This allows objects to perform cleanup operations before they are garbage collected. We learned about the purpose and usage of the “finalize” method. We have also seen the examples that demonstrate its implementation. By understanding the “finalize” method, you can ensure proper cleanup and resource management in your Java programs. Keep exploring and happy coding!