Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 31

final vs finally vs finalize in Java

In this lesson: Clear difference between final keyword, finally block and finalize method in Java — table and examples. In Hindi & English.

Three similar-looking words

final, finally and finalize look alike but are completely unrelated. final is a keyword, finally is a block, and finalize is a method. This is a favourite interview question, so it helps to know each one clearly.

💡 final = keyword (no change). finally = block (always runs). finalize = old method (deprecated).

final — the keyword

final int MAX = 100;
// MAX = 200;   // ERROR: cannot change a final variable

final class Utility { }        // cannot be extended
// final method cannot be overridden

final prevents change. A final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended. It is about making things constant or fixed.

finally — the block

try {
    int x = 5 / 0;
} catch (ArithmeticException e) {
    System.out.println("Caught error");
} finally {
    System.out.println("Always runs");
}
Caught error
Always runs

The finally block runs after try-catch no matter what — whether an exception happened or not, and even if there is a return. It is used for cleanup like closing files.

finalize — the method

// finalize() was called before garbage collection
// protected void finalize() { ... }
// DEPRECATED — do not use in modern Java

finalize() was a method the garbage collector called before destroying an object, meant for last-minute cleanup. It was unreliable and is now deprecated. Modern code uses try-with-resources instead.

Side-by-side

Pointfinalfinallyfinalize
What it isKeywordBlockMethod
PurposePrevent changeAlways-run cleanupPre-GC cleanup
Used withvar/method/classtry-catch(deprecated)
Common todayYesYesNo

What you actually use

  • final — used often, for constants and to lock down classes/methods.
  • finally — used for cleanup, though try-with-resources often replaces it.
  • finalize — do not use; it is deprecated.

So in real code you use final and finally regularly, and avoid finalize entirely.

Common mistakes

  • Thinking they are related. They share letters, not purpose.
  • Reassigning a final variable. That causes a compile error.
  • Expecting finally to be skipped. It runs even after a return in try.
  • Using finalize. It is deprecated — use try-with-resources.

Summary

  • final is a keyword that prevents change (variable, method, class).
  • finally is a block that always runs after try-catch.
  • finalize was a pre-garbage-collection method, now deprecated.
  • Use final and finally regularly; avoid finalize.
  • They only look similar — their jobs are unrelated.

Next, handle errors with custom exceptions, or learn cleanup of memory in garbage collection.

Frequently Asked Questions

What is the difference between final, finally and finalize?
They are unrelated despite similar names. final is a keyword that makes something unchangeable. finally is a block that always runs after try-catch. finalize was a method called before garbage collection (now deprecated).
What does the final keyword do?
final makes something constant: a final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended. It is about preventing change.
What is the finally block used for?
The finally block runs after a try-catch whether or not an exception happened. It is used for cleanup that must always run, like closing a file or database connection.
Is finalize still used in Java?
No, finalize() is deprecated and should not be used. It was meant to run before an object was garbage-collected, but it was unreliable. Use try-with-resources or explicit cleanup instead.
Does finally run even if there is a return in try?
Yes. The finally block runs even if the try or catch block has a return, throws an exception, or breaks. This is exactly why it is used for guaranteed cleanup.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicCustom Exceptions Your progress: 31 / 61 (51%) · Lesson 32 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free