final vs finally vs finalize()
This is a classic Java interview question that tests understanding of keywords, exception handling, and garbage collection. Though they sound similar, they serve completely different purposes.
High-Level Difference (One Line)
- final → Prevents change (variables, methods, classes)
- finally → Ensures execution of cleanup code
- finalize() → Cleanup before garbage collection (deprecated)
1️⃣ final Keyword
What Is final?
final is a keyword used to restrict modification.
It can be applied to:
- Variables
- Methods
- Classes
final Variable
final int x = 10;
x = 20; // ❌ compile-time error
- ✔ Value cannot be changed
- ✔ Used for constants
final Method
class Parent {
final void show() {}
}
class Child extends Parent {
// void show() {} ❌ cannot override
}
- ✔ Prevents method overriding
final Class
final class Utility {}
class Test extends Utility {} // ❌ not allowed
- ✔ Prevents inheritance
- ✔ Used for immutability (String)
2️⃣ finally Block
What Is finally?
finally is a block used with try–catch to execute important cleanup code.
✔ Executes always (almost)
Syntax
try {
// risky code
} catch (Exception e) {
// handling
} finally {
// cleanup code
}
Example
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception");
} finally {
System.out.println("Always executes");
}
✔ Executes whether exception occurs or not
When finally Does NOT Execute (Interview Trap)
- System.exit()
- JVM crash
- Power failure
3️⃣ finalize() Method
What Is finalize()?
finalize() is a method of the Object class that is called by the Garbage Collector before object destruction.
protected void finalize() throws Throwable {
// cleanup
}
Example
class Test {
protected void finalize() {
System.out.println("Finalize called");
}
}
- ✔ Called before garbage collection
- ❌ Not guaranteed when or if it runs
Important Note (Modern Java)
- ⚠ finalize() is deprecated (Java 9+)
- ✔ Unreliable
- ✔ Performance issues
- ✔ Use try-with-resources / AutoCloseable instead
finalize() vs Cleanup (Modern Practice)
❌ Avoid:
finalize()
✔ Prefer:
try-with-resources
try (FileInputStream fis = new FileInputStream("a.txt")) {
// use resource
}
Side-by-Side Comparison (Interview Favorite)
| Aspect | final | finally | finalize() |
|---|---|---|---|
| Type | Keyword | Block | Method |
| Used for | Restriction | Cleanup | GC cleanup |
| Related to | OOP | Exception handling | Garbage Collection |
| Execution | Compile-time | Runtime | GC dependent |
| Overridable | ❌ No | ❌ N/A | ✔ Yes |
| Recommended today | ✔ Yes | ✔ Yes | ❌ No (deprecated) |
Common Interview Traps
- Confusing finally with finalize()
- Thinking finalize() always runs
- Assuming finally executes after System.exit()
- Believing final makes objects immutable (only references)
Interview-Ready Answers
Short Answer
final restricts modification, finally ensures execution of cleanup code, and finalize() is called by the garbage collector before object destruction.
Detailed Answer
In Java, final is a keyword used to restrict inheritance, overriding, or reassignment. finally is a block associated with exception handling that executes regardless of whether an exception occurs. finalize() is a method of the Object class that was intended for cleanup before garbage collection, but it is deprecated due to unpredictability.
Key Takeaway
final → restriction
finally → guaranteed execution
finalize() → unreliable, deprecated
Mastering this distinction is mandatory for Core Java interviews and demonstrates strong
fundamentals.