Languages Comparisonbeginner

final vs finally vs finalize in Java

final is a keyword, finally is a block and finalize is a deprecated method. The names look alike but the jobs are different. See each with real code and output.

5 min read

Our verdict in 30 seconds

final is a keyword that stops change: a final variable cannot be reassigned, a final method cannot be overridden and a final class cannot be extended. finally is a block after try or catch that runs whether or not an exception occurs, used for cleanup. finalize is a method of Object that the garbage collector may call before removing an object; it is deprecated and should not be used in new code.

On This Page

final vs finally vs finalize is a classic Java interview question because the three words sound alike. They have nothing to do with each other. final is a keyword that prevents change. finally is a block that runs cleanup code after try. finalize is an old method the garbage collector may call before deleting an object. Learn one short example of each and you can answer this in under a minute.

final vs finally vs finalize: the comparison table

Point final finally finalize
What it is Keyword (modifier) Block Method of java.lang.Object
Used with Variables, methods, classes try or try-catch Objects, called by the garbage collector
Purpose Stop reassignment, overriding or inheritance Run cleanup code every time Last cleanup before an object is collected
When it acts At compile time When the try block exits Maybe, when GC runs; no guarantee
Who triggers it The compiler checks it The JVM runs it The garbage collector calls it
Status today In everyday use In everyday use Deprecated; avoid

1. final: the keyword that stops change

final works in three places.

Applied to Meaning Example
Variable Value can be set only once final int MAX = 100;
Method Subclasses cannot override it final String describe()
Class No class can extend it final class String in the JDK

Here is all three in one program:

import java.util.ArrayList;
import java.util.List;

public class FinalKeyword {
    static final double GST_RATE = 0.18;      // final variable: a constant

    static class Shape {
        final String describe() {             // final method: cannot be overridden
            return "I am a shape";
        }
    }

    static final class Circle extends Shape { // final class: cannot be extended
    }

    public static void main(String[] args) {
        System.out.println("GST on 1000: " + 1000 * GST_RATE);
        System.out.println(new Circle().describe());

        final List<String> names = new ArrayList<>();
        names.add("Ravi");                    // allowed: the list itself can change
        System.out.println(names);
        // names = new ArrayList<>();         // not allowed: the reference is final
    }
}

Output:

GST on 1000: 180.0
I am a shape
[Ravi]

If you try to reassign a final variable, the code does not compile:

public class FinalDemo {
    public static void main(String[] args) {
        final int MAX_MARKS = 100;
        MAX_MARKS = 120;
    }
}
FinalDemo.java:4: error: cannot assign a value to final variable MAX_MARKS
        MAX_MARKS = 120;
        ^
1 error

Common mistake: Thinking a final list cannot change. final only stops the variable from pointing to a different list. To block changes to the contents, use an unmodifiable list such as List.of(...).

2. finally: the block that always runs

finally comes after try (and optional catch). Its code runs whether the try block succeeds, throws an exception, or even returns. That makes it the right place to close files, database connections or other resources.

public class FinallyDemo {
    static int divide(int a, int b) {
        try {
            return a / b;
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
            return -1;
        } finally {
            System.out.println("finally runs for " + a + "/" + b);
        }
    }

    public static void main(String[] args) {
        System.out.println("Result: " + divide(10, 2));
        System.out.println("Result: " + divide(10, 0));
    }
}

Output:

finally runs for 10/2
Result: 5
Error: / by zero
finally runs for 10/0
Result: -1

Notice the first call: return a / b; runs, yet "finally runs" prints before "Result: 5". The return value is computed, the finally block runs, and only then does the method hand the value back.

The one common case where finally does not run is when the JVM itself stops, for example with System.exit(0) inside try.

In modern Java, try-with-resources closes resources for you and is usually cleaner than a hand-written finally:

try (BufferedReader reader = new BufferedReader(new FileReader("marks.txt"))) {
    System.out.println(reader.readLine());
}   // reader is closed here automatically

3. finalize: the method you should not rely on

finalize() is a protected method in Object. The idea was: before the garbage collector removes an object, it calls finalize() so the object can clean up.

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

    public static void main(String[] args) {
        new FinalizeDemo();   // object with no reference: eligible for garbage collection
        System.gc();          // only a request; the JVM may ignore it
        System.out.println("main ends");
    }
}

Compiling on Java 17 with -Xlint:deprecation shows a warning:

FinalizeDemo.java:3: warning: [deprecation] finalize() in Object has been deprecated
    protected void finalize() throws Throwable {
                   ^
1 warning

When we ran it on Java 17, it printed:

main ends
finalize called

But this output is not guaranteed. System.gc() is only a request. On another JVM, another garbage collector or another run, "finalize called" may print in a different order or never print at all. That uncertainty is exactly why finalize was deprecated.

Problems with finalize:

  • no guarantee when, or whether, it runs
  • it slows down garbage collection
  • an exception inside it is silently ignored
  • it can accidentally bring a dead object back to life

Since Java 9, finalize has been deprecated. Java 18 went further and deprecated finalization for removal (JEP 421). For cleanup, use try-with-resources, or java.lang.ref.Cleaner for advanced cases.

Memory trick

  • final = fixed. Nothing changes after it is set.
  • finally = always. Cleanup that runs at the end of try.
  • finalize = forget it. Old, unreliable, deprecated.

How interviewers ask this, and follow-ups to expect

The main question is usually "What is the difference between final, finally and finalize?" Good follow-ups to prepare:

  1. Does finally run if there is a return in try? Yes, before the method actually returns.
  2. When does finally not run? When the JVM exits, for example System.exit().
  3. Can a constructor be final? No. Constructors are not inherited, so final makes no sense there and is a compile error.
  4. What is a blank final variable? A final field with no value at declaration that must be set in every constructor.
  5. Why is String final? So no subclass can change its behaviour, which keeps strings safe to share and use as HashMap keys.

Where to go next

For the full picture of try, catch and finally, read exception handling in Java. To understand what the garbage collector really does, read garbage collection in Java. For final methods and classes in context, see inheritance. Then test yourself with Java exception handling interview questions and garbage collection interview questions.

Interview note: Answer in three lines: "final is a keyword that makes a variable constant, a method non-overridable and a class non-inheritable. finally is a block that runs after try whether or not an exception occurs, used for cleanup. finalize is a deprecated Object method that GC may call before collecting an object; we use try-with-resources or Cleaner instead." Then show the finally-with-return example if they ask for code.

Frequently Asked Questions

Does finally always run?
Almost always. It runs after a normal finish, after a caught exception, after an uncaught exception and even after a return statement inside try. It does not run if the JVM stops first, for example through System.exit() or a crash.
Is finalize removed from Java?
Not yet. It has been deprecated since Java 9, and Java 18 deprecated finalization for removal (JEP 421). It still compiles with a warning, but you should use try-with-resources or java.lang.ref.Cleaner instead.
Can a final variable be changed?
The variable cannot point to something else once set. But if it refers to an object such as a list, the object's contents can still change. final fixes the reference, not the object.
Can we override a final method?
No. A subclass that tries to override a final method gets a compile error. A final method can still be inherited and called.
Can finally be used without catch?
Yes. try followed directly by finally is valid. The exception, if any, still goes up to the caller after the finally block runs.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Explore the Java Full Stack program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn

More comparisons

View all
Chat with us