Difference Between Final, Finally and Finalize

Naveen AnamaneniUpdated · 4 min read

This is one of the most common Java interview questions, mainly because the three words look alike. In fact they have nothing to do with each other: final is a keyword that stops things from changing, finally is a block that always runs after a try, and finalize() is an old cleanup method that is now deprecated. One short program shows all three.

All Three in One Program

ThreeWords.java▶ Try it
public class ThreeWords {
    static final double GST_RATE = 0.18;
 
    public static void main(String[] args) {
        final String customer = "Navika";
        try {
            double bill = 1000 * (1 + GST_RATE);
            System.out.println(customer + " pays Rs. " + bill);
        } finally {
            System.out.println("Receipt closed");
        }
    }
}
Output
Navika pays Rs. 1180.0
Receipt closed

How it works

  1. final makes GST_RATE a constant and customer a variable that can't be reassigned.
  2. finally prints "Receipt closed" after the try, as it would even if the try had thrown.
  3. finalize() doesn't appear, and shouldn't: it's deprecated for removal (see "Java Finalize Method (Deprecated)").

Side by Side

finalfinallyfinalize()
What it isA keyword (modifier)A block in exception handlingA method of Object
Used onVariables, methods, classesAfter try or catchOverridden in a class
PurposePrevent change: no reassigning, overriding or extendingCleanup that must always runCleanup before garbage collection
When it actsCompile time (checked by the compiler)Every time the try block endsMaybe, someday, if the object is collected
StatusWidely usedWidely used; try-with-resources is often betterDeprecated for removal since Java 9/18
Learn more"Java Final Keyword""Finally Block in Java""Java Finalize Method (Deprecated)"

What final Does in Each Place

final on a...Means
VariableAssigned once; can't be reassigned
MethodCan't be overridden
ClassCan't be extended (like String)

Common Mistakes

Saying finally is a keyword like final. It is a keyword, but it introduces a block; it's not a modifier.

Recommending finalize() for cleanup. It's unreliable and deprecated; use try-with-resources.

Thinking a final object can't change. A final reference can't point elsewhere, but the object it points to can still change, unless it's immutable.

Interview Corner

What is the difference between final, finally and finalize?

final is a modifier: a final variable can be assigned only once, a final method can't be overridden, and a final class can't be extended. finally is a block after try or catch that runs whether or not an exception occurred, used for cleanup. finalize() is a method of Object that the garbage collector could call before reclaiming an object; it's unreliable and deprecated for removal, so resources should be released with try-with-resources instead.

Practice

  1. Easy: Write one line of code showing each of: a final variable, a final method, a final class, and a finally block.
  2. Harder: Try to reassign a final variable inside a finally block. What does the compiler say?

Quick check: final, finally, finalize

4 questions
1Which one stops a variable from being reassigned?
2Which one always runs after try?
3Which one is deprecated for removal?
4What does a final class prevent?

Key Takeaways

Was this tutorial helpful?

Lesson 234 of 482