Difference Between final, finally and finalize
final vs finally vs finalize in Java: a keyword, a cleanup block, and a deprecated method compared with clear code, common mistakes and interview answers.
Expected Interview Answer
final is a keyword that makes a variable constant, a method non-overridable, or a class non-inheritable; finally is a block that always runs after try/catch for cleanup; and finalize() is a deprecated Object method the garbage collector once called before reclaiming an object.
The three only look similar; they operate at different levels. final is a compile-time modifier enforcing immutability and preventing overriding or extension. finally is control-flow that guarantees cleanup code runs whether or not an exception is thrown, which is why try-with-resources or finally is used to close streams. finalize() was intended for last-chance cleanup before garbage collection but is unpredictable and was deprecated in Java 9; use try-with-resources or Cleaner instead.
- final enforces immutability and safer, clearer contracts
- final methods and classes prevent unwanted overriding or inheritance
- finally guarantees cleanup even when exceptions occur
- Understanding finalize() avoids relying on a deprecated, unreliable mechanism
- Encourages try-with-resources as the modern cleanup approach
AI Mentor Explanation
final is like a fixed boundary rope that cannot be moved once the match starts, locking the field in place. finally is like the mandatory pitch inspection that happens after every innings no matter how the innings ended, win or collapse. finalize is like an old groundsman who might tidy the pitch someday before it is dug up, but nobody knows when he will show up, so you never depend on him.
Step-by-Step Explanation
Step 1
final on variables
A final variable can be assigned once; after that its value (or reference) cannot change.
Step 2
final on methods and classes
A final method cannot be overridden and a final class cannot be extended.
Step 3
finally block
Placed after try/catch, it always executes, making it ideal for closing resources.
Step 4
finalize() method
An Object method the GC could call before reclaiming an object; deprecated since Java 9 and unreliable.
Step 5
Modern cleanup
Prefer try-with-resources or java.lang.ref.Cleaner over finalize() for deterministic cleanup.
What Interviewer Expects
- Distinguishing a keyword, a block, and a method clearly
- Explaining final for variables, methods, and classes
- Knowing finally always runs for cleanup
- Awareness that finalize() is deprecated and unreliable
- Recommending try-with-resources as the modern alternative
Common Mistakes
- Confusing finally with finalize()
- Thinking final makes an object's internal state immutable (it only fixes the reference)
- Believing finally never runs after a return statement
- Relying on finalize() for critical resource cleanup
- Assuming a final class can still be subclassed
Best Answer (HR Friendly)
“They sound alike but are unrelated: final locks something so it cannot change, finally is a block of code that always runs to clean up after a try/catch, and finalize was an old method the garbage collector used before removing an object. Today developers use final and finally regularly and avoid finalize.”
Code Example
public class KeywordDemo {
// final variable: assigned once, cannot change
private static final int MAX = 100;
// final method: cannot be overridden by subclasses
public final void printMax() {
System.out.println("MAX = " + MAX);
}
public void readFile() {
try {
System.out.println("reading...");
throw new RuntimeException("boom");
} catch (RuntimeException e) {
System.out.println("handled: " + e.getMessage());
} finally {
// always runs: perfect for cleanup
System.out.println("cleanup in finally");
}
}
}import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// resource is closed automatically, no finalize() needed
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
System.out.println("io error: " + e.getMessage());
}Follow-up Questions
- Can a finally block override a return value from try or catch?
- Why was finalize() deprecated and what replaces it?
- Does making a reference final make the referenced object immutable?
- How does try-with-resources decide the order in which resources close?
- Can a constructor or an abstract method be declared final?
MCQ Practice
1. Which one is a keyword used to prevent method overriding?
final is the keyword; a final method cannot be overridden and a final class cannot be extended.
2. When does a finally block execute?
finally runs whether or not an exception occurs, which makes it ideal for cleanup code.
3. What is the status of the finalize() method in modern Java?
finalize() was deprecated in Java 9 due to unpredictable timing; use try-with-resources or Cleaner instead.
Flash Cards
What is final? — A keyword making a variable constant, a method non-overridable, or a class non-inheritable.
What is finally? — A block after try/catch that always executes, typically used for resource cleanup.
What is finalize()? — A deprecated Object method the GC could call before collecting an object; unreliable, avoid it.
Modern cleanup alternative? — try-with-resources or java.lang.ref.Cleaner for deterministic resource release.