← Back to Java

final vs finally vs finalize()

JavaEntryjava

The Question

What is the difference between final, finally, and finalize() in Java?

What a Strong Answer Covers

  • final = modifier for variables/methods/classes
  • finally = exception handling block always executes
  • finalize() = GC cleanup method on Object
  • three distinct keywords

Senior-Level Answer

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.

Key Differences

Aspectfinalfinallyfinalize()
TypeKeyword/modifierBlock keywordMethod on Object
Applies tovariables, methods, classestry-catch constructObject instances
PurposePrevent reassignment/override/subclassingGuaranteed cleanup executionPre-GC resource cleanup
When executedCompile-time enforcedAfter try/catch exitsPossibly never (GC-driven)
ReliabilityAbsolute (compile-time)Near-certain (runtime)Unreliable
Modern usageCommon and encouragedCommon (prefer try-with-resources)Deprecated — avoid

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly defines all three with accurate descriptions of their respective purposes and contexts.

3/3 — Strong Answer

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.

Common Mistakes

  • Thinking a final object reference means the object is immutable — the reference is fixed, not the object's state.
  • Claiming finally always executes — System.exit() and JVM crashes are valid exceptions.
  • Recommending finalize() for resource cleanup without mentioning its deprecation and unreliability.

Follow-Up Questions

  • What happens if both the try block and the finally block throw exceptions? — The exception from finally replaces the one from try — the original exception is lost unless manually suppressed.
  • Can you override a final method in a subclass by using reflection? — No — final is enforced at the JVM bytecode level for normal invocation; reflection cannot override it.
  • Why is try-with-resources preferred over explicit finally for closing resources? — It guarantees close() is called, handles suppressed exceptions correctly, and removes boilerplate null-checks.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

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
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout