Difference Between Final, Finally and Finalize
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
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");
}
}
}Navika pays Rs. 1180.0
Receipt closedHow it works
finalmakesGST_RATEa constant andcustomera variable that can't be reassigned.finallyprints "Receipt closed" after thetry, as it would even if thetryhad thrown.finalize()doesn't appear, and shouldn't: it's deprecated for removal (see "Java Finalize Method (Deprecated)").
Side by Side
final | finally | finalize() | |
|---|---|---|---|
| What it is | A keyword (modifier) | A block in exception handling | A method of Object |
| Used on | Variables, methods, classes | After try or catch | Overridden in a class |
| Purpose | Prevent change: no reassigning, overriding or extending | Cleanup that must always run | Cleanup before garbage collection |
| When it acts | Compile time (checked by the compiler) | Every time the try block ends | Maybe, someday, if the object is collected |
| Status | Widely used | Widely used; try-with-resources is often better | Deprecated 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 |
|---|---|
| Variable | Assigned once; can't be reassigned |
| Method | Can't be overridden |
| Class | Can'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
- Easy: Write one line of code showing each of: a final variable, a final method, a final class, and a finally block.
- Harder: Try to reassign a
finalvariable inside afinallyblock. What does the compiler say?
Key Takeaways
Was this tutorial helpful?