What is the difference between final, finally, and finalize in Java?
These three sound similar but are unrelated. final is a keyword that makes a variable constant, a method not overridable, or a class not extendable. finally is a block that always runs after a try and catch, used for cleanup. finalize was a method called before an object was garbage collected, and it is now deprecated and should not be used.
- final is a keyword that marks a variable, method, or class as unchangeable or not extendable.
- finally is a block that always runs after try and catch, used for cleanup.
- finalize was a method called before garbage collection and is now deprecated.
final: prevents change
final int MAX = 100; // cannot be reassigned
final class Config {} // cannot be extended
finally: always runs
A finally block runs whether or not an exception was thrown, which makes it perfect for releasing resources like files or database connections.
try {
// risky work
} catch (Exception e) {
// handle
} finally {
// always runs, close resources here
}
finalize: deprecated cleanup hook
finalize was meant to run before an object was collected, but it was unreliable and is now deprecated. Modern code uses try with resources or explicit close methods instead.
Interviewers ask this to test attention to detail. Answer each in one line and stress that finalize is deprecated and you would never rely on it, which shows you know current best practice.
Frequently asked questions
Does the finally block always run?
Almost always, even when an exception is thrown or a value is returned. The rare exceptions are System.exit or the JVM crashing.
Why is finalize discouraged?
Its timing is unpredictable and it can slow down garbage collection. Modern code uses try with resources or explicit close methods instead.
Common follow up questions
Related interview questions
Want the full Java guide?
Read every Java concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the Java guide All Java questions