Open In App

finalize() Method in Java and How to Override it?

Last Updated : 02 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The finalize() method in Java is called by the Garbage Collector just before an object is destroyed. It allows the object to perform clean-up activity. Clean-up activity means closing the resources associated with that object like Database Connection, Network Connection, or we can say resource de-allocation.

  • It is defined in the Object class and available to all Java classes.
  • It is not a reserved keyword.
  • Used for clean-up activities like closing the database or network connections.
  • Called only once per object by the Garbage Collector.

What is Finalization in Java?

Just before destroying any object, the garbage collector always calls the finalize() method to perform clean-up activities on that object. This process is known as Finalization in Java. The Garbage collector calls the finalize() method only once on any object.

Note: The finalize() method has been deprecated since Java 9 and removed in Java 18 (JEP 421).

It is not recommended for use in modern Java applications. Prefer alternatives like try-with-resources, AutoCloseable, or java.lang.ref.Cleaner for resource management and cleanup.

Syntax of finalize()

protected void finalize() throws Throwable

Since the Object class contains the finalize method, hence finalize method is available for every Java class since Object is the superclass of all Java classes. Since it is available for every Java class, the Garbage Collector can call the finalize() method on any Java object.

Why finalize() Used? 

The finalize() method is used to:

  • Releases system resources before the garbage collector runs for a specific object. JVM allows finalize() to be invoked only once per object.
  • Performs any final cleanup tasks.

Note: The Garbage Collector calls the finalize() method only once per object, and there's no guarantee on when or even if it will be called.

How to Override finalize() Method 

The finalize method, which is present in the Object class, has an empty implementation. In our class, clean-up activities are there. Then we have to override this method to define our clean-up activities. In order to override this method, we have to define and call finalize within our code explicitly.

Example: Overriding finalize() Method

Java
// Java program to show the
// overriding of finalize() method
import java.lang.*;

// Defining a class Geeks since every java class
// is a subclass of predefined Object class
// Therefore Geeks is a subclass of Object class
// Java program to demonstrate overriding finalize()
public class Geeks {

    // Overriding finalize() to perform cleanup
    @Override
    protected void finalize() throws Throwable {
        try {
            System.out.println("Inside Geeks's finalize()");
        } catch (Throwable e) {
            throw e;
        } finally {
            System.out.println("Calling finalize() of Object class");
            
            // call to Object class finalize()
            super.finalize(); 
        }
    }

    public static void main(String[] args) throws Throwable {
        
        // Creating object
        Geeks g = new Geeks();

        // Manually calling finalize 
        g.finalize();
    }
}

Output
Inside Geeks's finalize()
Calling finalize() of Object class


Why Not Use finalize()?

This method may look useful, but finalize() is:

  • Unpredictable means there is no guarantee of execution.
  • It is deprecated starting from Java 9.
  • It is removed in Java 18 as part of JEP 421.

Alternatives we should use:

  • try-with-resources (It is recommended for file/stream handling)
  • AutoCloseable Interface
  • java.lang.ref.Cleaner class (introduced in Java 9)