What is the difference between final, finally, and finalize() in Java?
These three keywords sound similar but serve completely different purposes.
`final` is a non-access modifier with three meanings depending on context: - Applied to a variable: the reference cannot be reassigned after initialization. For primitives this makes the value constant. For object references, the reference is fixed but the object's internal state can still be mutated. - Applied to a method: the method cannot be overridden in subclasses. - Applied to a class: the class cannot be subclassed. `String`, `Integer`, and all other wrapper classes are final.
`finally` is a block that follows a `try` or `try-catch` block. Code in `finally` executes regardless of whether an exception was thrown or caught. It is used to guarantee cleanup — closing streams, releasing locks, etc. It does not execute in a few edge cases: `System.exit()`, a daemon thread being killed, or a JVM crash. Since Java 7, `try-with-resources` is the preferred alternative for resource cleanup, making explicit `finally` blocks for that purpose largely unnecessary.
`finalize()` is an instance method defined on `java.lang.Object`. The garbage collector may call it on an object before reclaiming the object's memory. It was intended for native resource cleanup but is unreliable, slow, and has been deprecated since Java 9. There is no guarantee it will run at all. Modern code should use `AutoCloseable` with try-with-resources or `java.lang.ref.Cleaner` instead.
The quick interview summary: `final` — immutability/constraint modifier. `finally` — always-runs cleanup block. `finalize()` — deprecated GC hook. They share etymology but no functional relationship.
| Aspect | final | finally | finalize() |
|---|---|---|---|
| Type | Keyword/modifier | Block keyword | Method on Object |
| Applies to | variables, methods, classes | try-catch construct | Object instances |
| Purpose | Prevent reassignment/override/subclassing | Guaranteed cleanup execution | Pre-GC resource cleanup |
| When executed | Compile-time enforced | After try/catch exits | Possibly never (GC-driven) |
| Reliability | Absolute (compile-time) | Near-certain (runtime) | Unreliable |
| Modern usage | Common and encouraged | Common (prefer try-with-resources) | Deprecated — avoid |
Correctly defines all three with accurate descriptions of their respective purposes and contexts.
Also covers the three uses of final (variable/method/class), the edge cases where finally doesn't run, and the deprecation of finalize() with modern alternatives.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment