Final vs Finally vs Finalize in Java: Complete Guide with Examples


If you are learning Java, you may have noticed three confusing terms: final, finally, and finalize(). Although they look very similar, they have completely different purposes. Understanding final vs finally vs finalize in Java is important for beginners, interview preparation, exams, and everyday Java programming.The easiest way to remember them is simple. The final keyword is used to restrict changes to variables, methods, and classes. The finally block is used with exception handling and is commonly associated with cleanup code. The finalize() method belongs to Java’s older finalization mechanism and is now deprecated for removal.

What Is final in Java?

The final keyword is used when you want to prevent something from being changed. In Java, final can be applied to variables, methods, and classes. Its behavior depends on where it is used.

When a variable is declared as final, it cannot be assigned a new value after its initialization.

final int MAX_USERS = 100;

System.out.println(MAX_USERS);

Trying to assign another value to MAX_USERS later will cause a compilation error.

final Variable in Java

A final variable can be assigned only once. This makes final useful when a value should not be reassigned during program execution. Constants are a common example of this concept.

class Settings {

    final int MAX_CONNECTIONS = 50;

    void display() {
        System.out.println(MAX_CONNECTIONS);
    }
}

One important point is that final does not automatically make an object immutable. If a final variable contains an object reference, the reference cannot be changed to point to another object, but the object’s internal state may still be modified when the object allows it.

final Method in Java

A method declared with the final keyword cannot be overridden by a subclass. This can be useful when a class wants to prevent subclasses from changing a particular implementation.

class Parent {

    final void showMessage() {
        System.out.println("This method cannot be overridden.");
    }
}

class Child extends Parent {

    // showMessage() cannot be overridden here
}

This behavior is especially relevant when working with inheritance. If a method contains logic that must remain unchanged in subclasses, declaring it final prevents method overriding.

final Class in Java

A class declared as final cannot be extended by another class.

final class Vehicle {

    void start() {
        System.out.println("Vehicle started");
    }
}

// This is not allowed:
// class Car extends Vehicle { }

This means final can be used at three important levels: a final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be inherited.

What Is finally in Java?

The finally keyword is related to exception handling. It is used to define a block of code that normally executes after the try and catch processing.

try {

    int result = 10 / 2;
    System.out.println(result);

} catch (ArithmeticException e) {

    System.out.println("An error occurred.");

} finally {

    System.out.println("Finally block executed.");
}

Unlike final, the finally keyword does not make a variable constant and does not prevent inheritance. It has a completely different purpose.

Why Is finally Used in Java?

The finally block is commonly associated with cleanup operations. For example, older Java code often used finally to close files, streams, database resources, or other resources after an operation was completed.

However, modern Java provides try-with-resources, which is generally a better approach when working with resources that implement AutoCloseable. It can automatically close resources and make cleanup code easier to manage.

What Is finalize() in Java?

finalize() is different from both final and finally. It was a method associated with Java’s object finalization mechanism.

class Demo {

    @Override
    protected void finalize() throws Throwable {
        System.out.println("Finalize called");
    }
}

However, developers should not use finalize() in new Java applications. The finalization mechanism has been deprecated for removal because it does not provide reliable and predictable resource management.

For modern Java development, explicit resource management and try-with-resources are preferred approaches. Developers working with advanced cleanup requirements should also examine the alternatives documented by the Java platform.

Why Is finalize() Deprecated?

The main problem with finalize() is that developers cannot reliably predict when, or even whether, finalization will happen before a program exits. It therefore should not be treated as a normal cleanup mechanism.

Using finalization can also introduce unnecessary complexity and performance concerns. Modern Java documentation recommends using safer alternatives instead of relying on finalization.

Final vs Finally vs Finalize in Java

Feature final finally finalize()
Type Keyword Block Method
Main purpose Restricts changes Exception-handling cleanup Legacy finalization
Used with Variables, methods, classes try/catch Object finalization
Can prevent reassignment? Yes No No
Can prevent method overriding? Yes No No
Can prevent inheritance? Yes No No
Modern recommendation Use when appropriate Use where appropriate Avoid in new code

Difference Between final, finally and finalize

The biggest difference between final, finally and finalize is their purpose. The final keyword controls whether something can be changed or extended. The finally block is part of Java’s exception-handling structure. The finalize() method was part of an older object-finalization mechanism.

There is also a syntax difference. final appears in variable, method, or class declarations. finally appears as a block after try or catch processing. finalize() contains parentheses because it is a method.

Simple Example to Understand All Three

final class Example {

    final int number = 10;

    void test() {

        try {

            System.out.println(number);

        } catch (Exception e) {

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

        } finally {

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

        }
    }
}

Here, the number variable is final, meaning it cannot be reassigned. The Example class is also final, so another class cannot extend it. The finally block belongs to exception handling and is separate from the final keyword.

final vs finally vs finalize for Java Interviews

The difference between final, finally, and finalize is a common Java interview topic. A simple interview answer is: final is a keyword used with variables, methods, and classes to restrict changes; finally is a block used with exception handling; and finalize() is an older finalization method that has been deprecated for removal.

If you are preparing for Java interviews, it is also useful to understand related exception-handling concepts such as throw vs throws in Java. Understanding how throw, throws, try, catch, and finally work together can make Java exception-handling questions much easier to answer.

final vs finally vs finalize: Common Mistakes

One common mistake is thinking that final and finally are interchangeable. They are not. A final variable and a finally block perform completely different tasks.

Another mistake is assuming that finalize() should be used whenever an object needs cleanup. That approach is outdated. Resource cleanup should normally be handled explicitly or through mechanisms such as try-with-resources.

Beginners also sometimes confuse final with immutability. Declaring an object reference final does not necessarily mean that the object itself can never change. It only prevents the reference from being assigned to another object.

When Should You Use final?

Use final when your design requires a variable to avoid reassignment, a method to avoid overriding, or a class to avoid inheritance.

For example, configuration values that should not be reassigned can be declared final. A method whose implementation must remain fixed in an inheritance hierarchy can also be final.

When Should You Use finally?

Use finally when you need exception-handling logic that should normally execute after the try/catch processing. For resource management in modern Java, however, consider try-with-resources when the resource implements AutoCloseable.

Should You Use finalize()?

For new Java applications, avoid finalize(). It is a legacy mechanism that has been deprecated for removal. If you are maintaining older Java code that depends on finalization, review the code and consider replacing the finalization-based approach with modern resource-management techniques.

Frequently Asked Questions

What is the difference between final and finally in Java?

final is a keyword used to restrict variables, methods, and classes. finally is a block used with exception handling and is normally executed after try/catch processing.

What is the difference between final and finalize()?

final is a Java keyword, while finalize() is a method associated with the older object-finalization mechanism. finalize() has been deprecated for removal.

Is finalize() still used in Java?

It may appear in legacy applications, but it should not be used for new application code. Modern Java provides better approaches for resource management.

Can a final method be overridden?

No. A final method cannot be overridden by a subclass.

Can a final class be inherited?

No. A class declared final cannot be extended by another class.

Conclusion

Understanding final vs finally vs finalize in Java becomes simple when you remember the role of each term. final is used to restrict reassignment, method overriding, and class inheritance. finally is an exception-handling block commonly used for cleanup logic. finalize() belongs to an older finalization mechanism and should be avoided in new Java applications because it has been deprecated for removal.

For Java beginners, remember this simple rule: final controls changes, finally handles exception-related cleanup, and finalize() is legacy functionality. Knowing this distinction will help with Java programming, exception handling, and technical interview preparation.