final vs finally vs finalize

Difficulty: Beginner

Question

What is the difference between final, finally and finalize in Java?

Answer

These three words look almost identical, which is exactly why interviewers use them: they check whether you actually know Java or just memorized keywords. Let me give each one its own identity, and then cover the tricky bits.

The keyword final is a modifier meaning once assigned, it cannot change. Its effect depends on where you use it. On a variable, it means the variable can be assigned only once. For a primitive, the value is fixed. For a reference, the reference is fixed but the object it points to can still be modified. A final List can still have elements added; you just cannot point it at another list. On a method, final prevents overriding by subclasses. On a class, final prevents inheritance, which is why String and Integer are final. A blank final field must be assigned exactly once, either at declaration or in every constructor.

The keyword finally is a block used with try. The code inside finally runs whether or not an exception occurred, and whether or not it was caught. It is the traditional place for cleanup such as closing files, connections or releasing locks. If the try block has a return statement, the finally block still runs before the method actually returns.

There are only a few cases where finally does not run: System.exit is called inside try, the JVM crashes or the process is killed, or the thread is stuck in an infinite loop or dies abruptly. Another well-known trap: if finally itself contains a return or throws an exception, it overrides whatever the try or catch produced, silently swallowing the original exception. That is considered bad practice and static analysis tools flag it.

Also note what a return inside try captures. If try returns a primitive variable and finally then modifies the variable, the caller still gets the old value, because the return value was already evaluated and stored. For a mutable object, though, changes to the object made in finally are visible.

The method finalize is a protected method in Object that the garbage collector could call before reclaiming an object, giving it a last chance to release resources. It sounds useful but has serious problems: no guarantee of when (or even if) it runs, it slows down GC, it can resurrect objects, and exceptions inside it are ignored. It was deprecated in Java 9 and marked for removal in Java 18 as part of JEP 421. The modern replacements are try-with-resources with AutoCloseable for deterministic cleanup, and java.lang.ref.Cleaner or PhantomReference for safety-net cleanup.

An easy way to remember: final is about immutability (a keyword), finally is about exceptions (a block), and finalize is about garbage collection (a method).

A common follow-up: can a final method be overloaded? Yes. Only overriding is blocked. Another: can a final variable be modified in a constructor? Only if it is a blank final that has not yet been assigned. A final static constant, by convention named in upper snake case, must be assigned at declaration or in a static initializer.

Code examples

final references and finally with return

import java.util.*;

public class ThreeFs {
    static String test() {
        try {
            return "try";
        } finally {
            System.out.println("cleanup");
        }
    }

    public static void main(String[] args) {
        final List<String> names = new ArrayList<>();
        names.add("Asha");
        System.out.println(names);
        // names = new ArrayList<>();  // compile error: cannot assign a value to final variable

        System.out.println(test());
    }
}

The final list can still be mutated, only the reference is locked. The finally block prints cleanup before the method hands back the try value.

Key points

Common follow-up questions

Interview tips

Concepts covered

final, finally, finalize, Exception Handling, Garbage Collection

More Core Java & Collections questions