Q17: What are the differences between final, finally, and finalize?

  • final: Keyword modifier.
    • final variable: Constant (value cannot be reassigned).
    • final method: Prevents method overriding.
    • final class: Prevents inheritance.
  • finally: Block paired with try-catch to execute cleanup code unconditionally.
  • finalize: Deprecated Object method invoked by Garbage Collector prior to object reclamation.
Key Takeaways
  • final restricts mutability/inheritance.
  • finally guarantees cleanup execution.
  • finalize() is deprecated—use Cleaner or AutoCloseable instead.
Knowledge Check

Will a finally block execute if a try block contains a return statement?