← Back to Questions
Java

Difference between final, finally, and finalize?

Learn Difference between final, finally, and finalize? with simple explanations, real-time examples, interview tips and practical use cases.

Difference Between final, finally, and finalize in Java

In Java, final, finally, and finalize() are completely different concepts even though their names look similar.

In simple words:

  • final → Used for restrictions
  • finally → Used for exception cleanup
  • finalize() → Used by Garbage Collector before object destruction

Overview Diagram


Java Concepts

     |
     +-------> final
     |
     +-------> finally
     |
     +-------> finalize()


What is final in Java?

The final keyword is used to restrict modification.


Used With

  • Variables
  • Methods
  • Classes

Examples

final Variable

final int x = 100;

Value cannot be changed.


final Method

final void display() {

}

Method cannot be overridden.


final Class

final class Test {

}

Class cannot be inherited.


final Keyword Flow


Member Declared final

      |
      v

Modification Restricted


What is finally in Java?

The finally block is used in exception handling.

It always executes whether exception occurs or not.


Example

try {

    int result = 10 / 0;

}
catch(Exception e) {

    System.out.println(
        "Exception Handled"
    );

}
finally {

    System.out.println(
        "Cleanup Code"
    );

}

Output


Exception Handled

Cleanup Code


finally Block Flow


try Block Executes

      |
      +-------> Exception?
              |
              v
          catch Block

      |
      v

finally Block Executes


Why finally is Used?

  • Close database connections
  • Release resources
  • Close files
  • Cleanup operations

What is finalize() in Java?

The finalize() method is called by Garbage Collector before destroying an object.


Example

class Test {

    @Override
    protected void finalize()
    throws Throwable {

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

    }

}

finalize() Flow


Object Becomes Unreachable

      |
      v

Garbage Collector Detects Object

      |
      v

finalize() Executes

      |
      v

Object Removed from Memory


Important Modern Java Note

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


Main Difference Table

Feature final finally finalize()
Type Keyword Block Method
Purpose Restriction Cleanup Code Garbage Collection Cleanup
Used In Variables, Methods, Classes Exception Handling Object Class
Execution Compile Time Restriction After try-catch Before Object Destruction
Called By Programmer Program Flow Garbage Collector
Inheritance Impact Prevents Inheritance/Overriding No Impact No Impact
Modern Usage Widely Used Widely Used Deprecated

Simple Real-Time Understanding

Concept Real-Life Meaning
final Permanent Rule
finally Mandatory Cleanup Work
finalize() Cleanup Before Throwing Away Object

Real-Time Banking Example

final

final double MIN_BALANCE = 1000;

Minimum balance should never change.


finally

finally {

    connection.close();

}

Database connection must close after transaction.


finalize()

Older systems used finalize() to release native resources before object cleanup.


Real-Time E-Commerce Example

final

final String TAX_ID = "GST123";

finally

finally {

    paymentSession.close();

}

finalize()

Older applications used finalize() for cache cleanup.


Modern Enterprise Usage

Concept Modern Enterprise Usage
final Very Common
finally Common for Cleanup
finalize() Avoided/Deprecated

Spring Boot Usage

final in Spring Boot

private final UserRepository repo;

Dependency should not change.


finally in Spring Boot

finally {

    stream.close();

}

finalize() in Spring Boot

Modern Spring Boot applications avoid finalize().


Microservices Usage

Microservices architectures use:

  • final → Immutable configurations
  • finally → Resource cleanup
  • finalize() → Rarely used

Internal Working Comparison


final

    |
    +-------> Restriction Applied


finally

    |
    +-------> Cleanup Executes


finalize()

    |
    +-------> GC Cleanup Attempt


Advantages of final

  • Improves security
  • Supports immutability
  • Prevents accidental modification

Advantages of finally

  • Ensures cleanup execution
  • Prevents resource leaks
  • Improves stability

Disadvantages of finalize()

  • Deprecated
  • Unpredictable execution
  • Poor performance
  • Security risks

Common Interview Mistake

Many developers confuse:

  • finally block with finalize() method

Actually:

  • finally → Exception handling cleanup
  • finalize() → Garbage Collection cleanup

Another Common Mistake

Many developers think finalize() always executes.

Actually:

  • Garbage Collection timing is not guaranteed.

Best Practices

  • Use final for constants and immutable design
  • Use finally for cleanup logic
  • Avoid finalize() in modern Java
  • Prefer try-with-resources instead of finalize()

Modern Java Alternative to finalize()

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

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

}

Why Better?

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

Related Learning Topics


Professional Interview Answer

In Java, final, finally, and finalize() are different concepts. The final keyword is used to restrict modification of variables, methods, and classes. The finally block is used in exception handling to execute cleanup code regardless of whether an exception occurs or not. The finalize() method is called by the Garbage Collector before destroying an object, but it is deprecated in modern Java because of unreliable execution and poor performance. Modern enterprise applications use final for immutability and security, finally for resource cleanup, and avoid finalize() in favor of try-with-resources and AutoCloseable.


Frequently Asked Questions

What is final in Java?

final is a keyword used to restrict modification.

What is finally in Java?

finally is a block used for cleanup in exception handling.

What is finalize() in Java?

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

Is finalize() deprecated?

Yes, finalize() is deprecated in modern Java.

Which is preferred for cleanup in modern Java?

try-with-resources is preferred for cleanup operations.

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.