0
0
JavaComparisonBeginner · 3 min read

Final vs Finally vs Finalize in Java: Key Differences and Usage

In Java, final is a keyword used to declare constants, prevent method overriding, or inheritance. finally is a block used to execute important code after try-catch, regardless of exceptions. finalize is a method called by the garbage collector before object destruction, now deprecated since Java 9.
⚖️

Quick Comparison

Here is a quick table summarizing the key differences between final, finally, and finalize in Java.

Aspectfinalfinallyfinalize
TypeKeywordBlockMethod
PurposeDeclare constants, prevent inheritance/overridingExecute code after try-catchCleanup before garbage collection
Usage LocationVariables, methods, classesTry-catch blocksIn a class, overridden from Object
Execution TimeCompile-time effectRuns after try-catch at runtimeRuns before object is garbage collected
Exception HandlingNoYes, always executes even if exception occursNo
StatusActive and widely usedActive and widely usedDeprecated since Java 9
⚖️

Key Differences

final is a keyword that you use to make a variable's value unchangeable, prevent a method from being overridden, or stop a class from being subclassed. For example, a final variable acts like a constant.

finally is a block that always runs after a try and catch block, no matter what happens. It is used to clean up resources like closing files or database connections.

finalize is a method that the Java garbage collector calls before destroying an object. It was meant to let you release resources, but it is unreliable and deprecated since Java 9. You should avoid using it and use other cleanup methods instead.

⚖️

Code Comparison

Example showing final usage to declare a constant and prevent method overriding.

java
public class FinalExample {
    final int constantValue = 10; // constant variable

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

    public static void main(String[] args) {
        FinalExample obj = new FinalExample();
        System.out.println("Constant value: " + obj.constantValue);
        obj.show();
    }
}
Output
Constant value: 10 This method cannot be overridden.
↔️

finally Equivalent

Example showing finally block used to close a resource after try-catch.

java
public class FinallyExample {
    public static void main(String[] args) {
        try {
            int result = 10 / 0; // causes ArithmeticException
        } catch (ArithmeticException e) {
            System.out.println("Exception caught: " + e.getMessage());
        } finally {
            System.out.println("This finally block always executes.");
        }
    }
}
Output
Exception caught: / by zero This finally block always executes.
🎯

When to Use Which

Choose final when you want to create constants or prevent changes to classes or methods. Use finally to ensure important cleanup code runs after try-catch blocks, like closing files or releasing resources. Avoid using finalize because it is deprecated and unreliable; instead, use try-with-resources or explicit cleanup methods.

Key Takeaways

final is for constants and preventing inheritance or overriding.
finally block always runs after try-catch for cleanup.
finalize is a deprecated method for cleanup before garbage collection.
Avoid finalize; prefer explicit resource management.
Use final and finally appropriately to write safer Java code.