Java final, finally and finalize

Last Updated : 30 Sep, 2026

In Java, final, finally, and finalize() are three distinct concepts with different purposes. The final keyword applies restrictions to variables, methods, and classes, while the finally block is used with exception handling. finalize() is a deprecated legacy method historically associated with object cleanup before garbage collection.

  • final restricts reassignment, method overriding, or class inheritance depending on where it is used.
  • finally is used to execute cleanup code after a try/catch construct in normal circumstances.
  • finalize() is deprecated, and its execution is not guaranteed or timely; it should not be used for resource cleanup.
final-finally
final, finally and finalize

final Keyword

The final keyword is used to restrict changes in Java. It can be applied to variables, methods, and classes. Once declared as final, certain modifications are not allowed.

  • A final variable cannot be reassigned after initialization.
  • A final method cannot be overridden by subclasses.
  • A final class cannot be inherited.

Syntax

// Constant value
final int b = 100;

Example: Program to demonstrates the value of the variable cannot be changed once initialized.

Java
class A {
    public static void main(String[] args)
    {
        // Non final variable
        int a = 5;

        // final variable
        final int b = 6;

        // modifying the non final variable
        a++;

        // modifying the final variable Immediately gives Compile Time error
        b++;
    }
}

Output:

Output

Note: If we declare any variable as final, we can't modify its value. Attempting to do so results in a compile-time error.

In the above example, we used the final keyword to declare a variable that cannot be modified after its initialization. Similarly, the final keyword can also be applied to methods and classes in Java to impose certain restrictions.

finally Keyword

The finally block is used in exception handling. It contains code that is executed after the try and catch blocks, regardless of whether an exception occurs or not.

  • Always executes after the try-catch block.
  • Used for cleanup tasks such as closing files or database connections.
  • Executes even if an exception is thrown.

Syntax

try {

// Code that might throw an exception

} catch (ExceptionType e) {

// Code to handle the exception

} finally {

// Code that will always execute

}

Example: Program to demonstrates the working of finally block in exception handling.

Java
public class Geeks {
    public static void main(String[] args)
    {
        try {
            System.out.println("Inside try block");
            int result
                = 10 / 0; // This will cause an exception
        }
        catch (ArithmeticException e) {
            System.out.println("Exception caught: "
                               + e.getMessage());
        }
        finally {
            System.out.println(
                "finally block always execute");
        }
    }
}

Output:

Output
output

Explanation: The try block attempts a division by zero, causing an ArithmeticException. The finally block executes, whether an exception occurs, ensuring cleanup or mandatory code execution.

finalize() Method

The finalize() method is a legacy method of the Object class that was historically associated with cleanup before garbage collection. However, its execution is not guaranteed or timely, so it should not be relied upon for resource management.

  • It was historically intended to perform cleanup before an object was reclaimed by garbage collection.
  • It is defined in the Object class.
  • It is deprecated and should not be used for resource cleanup in modern Java applications.

Syntax

protected void finalize() throws Throwable{}

Note: finalize() is deprecated in Java 9 and should not be used in modern applications. It’s better to use try-with-resources or other cleanup mechanisms instead of relying on finalize().

Example: Program demonstrates the working of finalize() method in context of garbage collection.

Java
import java.util.*;

public class Geeks {
    public static void main(String[] args)
    {
        Geeks g = new Geeks();

        System.out.println("Hashcode is: " + g.hashCode());

        // Making the object eligible for garbage collection
        g = null;

        System.gc();

        // Adding a short delay to allow GC to act
        try {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("End of the garbage collection");
    }

    // Defining the finalize method
    @Override protected void finalize()
    {
        System.out.println("Called the finalize() method");
    }
}

Output:

Output
Output

Explanation: In the above example, an object "g" is created and its hash code is printed. The object is made eligible for garbage collection by setting it to null and invoking System.gc().

final vs finally vs finalize

The table below illustrates the differences between "final", "finally" and "finalize."

Featurefinalfinallyfinalize()
TypeKeywordBlockMethod of the Object class
PurposeRestricts reassignment, method overriding, or class inheritanceUsed to execute cleanup or completion code associated with exception handlingLegacy mechanism historically associated with cleanup before garbage collection
Used WithVariables, methods, and classestry/catch exception-handling constructsObjects
BehaviorIts restriction is enforced based on where the keyword is appliedNormally executes when control leaves the associated try/catch constructExecution is not guaranteed or timely
Current StatusSupportedSupportedDeprecated; should not be used in new code
Comment