Skip to the page

Core Java and OOP · Concept

final, finally and finalize

The interviewer asks

What's the difference between final, finally and finalize? And what does static mean on a field, a method and a block?

Then

Follow-ups the interviewer asks next

  1. A list is declared final. Can you add items to it?
  2. When does a finally block not run?
  3. What happens if both try and finally contain a return?
  4. Can you override a static method?
  5. Why is finalize deprecated, and what do you use instead?
  6. Why can a static mutable field cause bugs in a web application?

The answer

What a strong answer covers

  • final variable: assigned once (the reference is fixed; the object can still change). final method: can't be overridden. final class: can't be extended (String, records).
  • finally: runs after try/catch whether or not an exception was thrown; used for cleanup. It doesn't run if the JVM exits (System.exit, crash, kill -9). A return in finally overrides the try's return and swallows exceptions: avoid it.
  • finalize: an old hook run by the GC before reclaiming an object, with no guarantee when or if. Deprecated since Java 9 and deprecated for removal since Java 18 (JEP 421). Use try-with-resources, or java.lang.ref.Cleaner as a safety net.
  • static: belongs to the class, not an instance. Static methods are hidden, not overridden (the call depends on the declared type). Static blocks run once when the class is initialised.
  • Static mutable state is shared by every request thread: races, and data leaking between users; it's also a GC root, so collections in static fields only grow.

The red pen

Common traps

Avoid:"final makes the object immutable"; "finally always runs, no exceptions"; "finalize is like a destructor".

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview