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 — 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");
}
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
| Point | final | finally | finalize |
|---|---|---|---|
| What it is | Keyword | Block | Method |
| Purpose | Prevent change | Always-run cleanup | Pre-GC cleanup |
| Used with | var/method/class | try-catch | (deprecated) |
| Common today | Yes | Yes | No |
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
finalis a keyword that prevents change (variable, method, class).finallyis a block that always runs after try-catch.finalizewas 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?
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?
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?
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?
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.