final vs finally vs finalize in Java
Fast recall
Quick revision
Core answer
Whether you can clearly separate three unrelated concepts that share a name root, and whether you know finalize is deprecated
Treating finalize as a reliable cleanup mechanism, or confusing final variables with immutability (a final reference can still point to a mutable object)
Mention that finalize is deprecated since Java 9, explain why (unpredictable timing, GC overhead, resurrection bugs), and name the replacements — Cleaner and PhantomReference
Interview-ready guide
Interview answer
final, finally, and finalize() are three unrelated Java keywords that share a common prefix — final is a type modifier, finally is a try-catch control-flow block, and finalize() was a deprecated lifecycle callback on Object. They appear together in interviews specifically because their similar names invite confusion.
final:
- Prevents reassignment for variables, overriding for methods, subclassing for classes
- Freezes the reference, not the object — a
final Listcan still have elements added; you just can't point the variable at a different list StringandIntegerare final for immutability and thread safety
finally:
- Runs after try/catch no matter what — success, exception, or even a return statement
- Only
System.exit()or a JVM crash skips it - In modern code, try-with-resources handles most cleanup; use
finallyfor non-AutoCloseable resources
finalize():
- Was a GC callback on
Objectthat might run before an object is reclaimed - Timing was completely unpredictable — seconds, minutes, or never; deprecated in Java 9
- The replacement is
CleanerwithPhantomReference
I recommend using final liberally for safety, try-with-resources instead of finally whenever possible, and never touching finalize().
final vs finally vs finalize in Java
Direct Answer
final is a modifier that prevents modification — final classes can't be subclassed, final methods can't be overridden, final variables can't be reassigned. finally is a block that always executes after try/catch regardless of exceptions. finalize() was a garbage-collection callback on Object that is deprecated since Java 9 and effectively removed — use Cleaner or PhantomReference instead.
Deep Dive & Architecture
Varargs (`Type...
args`) = syntactic sugar — compiler converts to array.
final is a modifier that prevents modification — final classes can't be subclassed, final methods can't be overridden, final variables can't be reassigned. finally is a block that always executes after try/catch regardless of exceptions. finalize() was a garbage-collection callback on Object that is deprecated since Java 9 and effectively removed — use Cleaner or PhantomReference instead.
Saying Java is pass-by-reference for objects because you can modify the object's fields inside a method
Common Mistakes & Pitfalls
Treating finalize as a reliable cleanup mechanism, or confusing final variables with immutability (a final reference can still point to a mutable object)
Check your understanding
Answer these without looking back at the article.
- Is Java pass-by-value or pass-by-reference?
- Varargs in Java — Variable Arguments and Heap Pollution
- What is autoboxing and unboxing in Java? What is the Integer cache trap?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.