Skip to main content
InterviewHubPRO
Section Overview
Q23

Question 23 of 50

Part 2: Advanced Concurrency, Memory & Modern Java Architecture (11 - 24)

46%
Java Core & JVM (50 Qs)•Part 2: Advanced Concurrency, Memory & Modern Java Architecture (11 - 24)

What are final, finally, and finalize, and why was finalize() deprecated?

senior LevelSystem Design

In Simple Words:

  • final: Keyword indicating immutability. A final variable cannot be reassigned; a final method cannot be overridden; a final class cannot be subclassed.
  • finally: Block following a try-catch block that is guaranteed to execute regardless of whether an exception is thrown or caught (except in System.exit()).
  • finalize(): Deprecated method on java.lang.Object originally intended to clean up resources before an object was garbage-collected.

Why finalize() was Deprecated in Java 9 & Removed:

  1. Unpredictable Execution: The JVM gives zero guarantee on when or if finalize() will run.
  2. Performance Degradation: Finalizable objects survive multiple GC generations, bloating young generation promotion and causing massive GC pauses.
  3. Object Resurrection: A poorly written finalizer can reassign this to a static variable, resurrecting an already dead object!
  4. Modern Replacement: Always use AutoCloseable with try-with-resources or java.lang.ref.Cleaner.