← Back to Questions
Java

What is finalize method in Java?

Learn What is finalize method in Java? with simple explanations, real-time examples, interview tips and practical use cases.

The finalize() method in Java is a method provided by the Object class that is called by the Garbage Collector before destroying an object.

In simple words:

finalize() is used to perform cleanup activities before an object is removed from memory.


Why finalize() Method was Important?

The finalize() method was historically used for:

  • Releasing resources
  • Closing native connections
  • Cleaning unmanaged memory
  • Performing cleanup before object destruction

Important Modern Java Note

The finalize() method is deprecated in modern Java versions because it is unreliable and inefficient.

Modern applications should use:

  • try-with-resources
  • AutoCloseable
  • Explicit cleanup methods

finalize() Method Syntax

class Test {

    @Override
    protected void finalize()
    throws Throwable {

        System.out.println(
            "finalize() called"
        );

    }

}

finalize() Method Execution Flow


Object Created

      |
      v

Object Becomes Unreachable

      |
      v

Garbage Collector Detects Object

      |
      v

finalize() Executes

      |
      v

Object Removed from Memory


Basic Example

class Employee {

    @Override
    protected void finalize()
    throws Throwable {

        System.out.println(
            "Object Destroyed"
        );

    }

    public static void main(String[] args) {

        Employee e = new Employee();

        e = null;

        System.gc();

    }

}

Possible Output


Object Destroyed


Why Output is "Possible"?

Because Garbage Collection timing is not guaranteed.


Important Point About finalize()

Calling:

System.gc();

only requests JVM for garbage collection.

JVM may ignore the request.


Internal Working of finalize()


Object Created

      |
      v

Object Becomes Unused

      |
      v

Garbage Collector Marks Object

      |
      v

finalize() Invoked

      |
      v

Memory Released


What is Garbage Collection?

Garbage Collection is the process of automatically removing unused objects from memory.


Garbage Collection Flow


Heap Memory

      |
      +-------> Reachable Objects

      |
      +-------> Unreachable Objects

                      |
                      v

              Garbage Collector Removes


Can finalize() be Called Manually?

Yes, but not recommended.


Example

Employee e = new Employee();

e.finalize();

Why Not Recommended?

Because finalize() is intended for Garbage Collector usage.


Can finalize() Execute Multiple Times?

Normally:

  • finalize() executes only once per object.

Can finalize() Prevent Object Destruction?

Yes, theoretically object resurrection is possible.


Example

class Test {

    static Test obj;

    @Override
    protected void finalize()
    throws Throwable {

        obj = this;

    }

}

What Happens Here?

Object becomes reachable again inside finalize().


Why Dangerous?

  • Memory leaks
  • Unpredictable behavior
  • Complex debugging

Why finalize() is Deprecated?

Modern Java deprecated finalize() because:

  • Unpredictable execution
  • Poor performance
  • Security risks
  • Memory overhead
  • Garbage Collection delays

Official Modern Alternatives

  • try-with-resources
  • AutoCloseable
  • Cleaner API

Modern Alternative Example

try(
    BufferedReader br =
        new BufferedReader(
            new FileReader("test.txt")
        )
) {

    System.out.println(br.readLine());

}

Why try-with-resources is Better?

  • Guaranteed cleanup
  • Better performance
  • Cleaner syntax
  • No dependency on GC timing

finalize() vs finally Block

Feature finalize() finally
Type Method Block
Used For Garbage Collection Cleanup Exception Cleanup
Called By Garbage Collector Program Flow
Execution Guarantee No Mostly Yes

final vs finally vs finalize()

Feature final finally finalize()
Type Keyword Block Method
Purpose Restriction Cleanup Logic Garbage Collection Cleanup
Used In Variables/Methods/Classes Exception Handling Object Cleanup

finalize() in Banking Systems

Older banking applications sometimes used finalize() for:

  • Closing native resources
  • Releasing file handlers
  • Cleaning unmanaged memory

Why Modern Banking Systems Avoid finalize()?

  • Unpredictable cleanup timing
  • Performance issues
  • Security concerns
  • Compliance requirements

finalize() in E-Commerce Systems

Older enterprise systems used finalize() for:

  • Temporary file cleanup
  • Cache cleanup
  • Session cleanup

Modern Enterprise Replacement

Modern systems use:

  • Connection pools
  • Resource managers
  • AutoCloseable
  • Container-managed cleanup

finalize() in Spring Boot

Modern Spring Boot applications avoid finalize().


Why?

  • Spring manages resources automatically
  • Bean lifecycle management exists
  • Cleaner shutdown hooks available

Spring Boot Alternatives

@PreDestroy
public void cleanup() {

}

Microservices and finalize()

Cloud-native microservices avoid finalize() because:

  • Containers require predictable cleanup
  • GC timing is unreliable
  • Performance is critical

Advantages of finalize()

  • Automatic cleanup support
  • Fallback cleanup mechanism

Disadvantages of finalize()

  • Deprecated
  • Unpredictable execution
  • Poor performance
  • Security risks
  • Memory overhead
  • Difficult debugging

Common Interview Mistake

Many developers think finalize() executes immediately after object becomes null.

Actually:

  • finalize() depends on Garbage Collector execution.

Another Common Mistake

Many developers think System.gc() guarantees finalize() execution.

Actually:

  • System.gc() only requests garbage collection.

Best Practices

  • Avoid finalize() in modern Java
  • Use try-with-resources
  • Use AutoCloseable
  • Use explicit cleanup methods
  • Use @PreDestroy in Spring Boot

Realtime Enterprise Example

Modern Resource Cleanup

class DatabaseConnection
implements AutoCloseable {

    @Override
    public void close() {

        System.out.println(
            "Connection Closed"
        );

    }

}

Related Learning Topics


Professional Interview Answer

The finalize() method in Java is a method of the Object class that is called by the Garbage Collector before removing an object from memory. It was traditionally used for cleanup operations such as releasing unmanaged resources and closing native connections. However, finalize() is deprecated in modern Java because its execution is unpredictable and inefficient. Modern enterprise applications prefer try-with-resources, AutoCloseable, Cleaner API, and framework-managed lifecycle methods for reliable resource management. Spring Boot and cloud-native microservices typically avoid finalize() entirely due to performance and reliability concerns.


Frequently Asked Questions

What is finalize() method in Java?

finalize() is a method called by Garbage Collector before object destruction.

Is finalize() deprecated?

Yes, finalize() is deprecated in modern Java versions.

Does System.gc() guarantee finalize() execution?

No, System.gc() only requests garbage collection.

What is the modern alternative to finalize()?

try-with-resources and AutoCloseable are modern alternatives.

Why is finalize() not recommended?

Because it is unreliable, slow, and dependent on Garbage Collector timing.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.