Explain the difference between final, finally, and finalize in Java

๐Ÿ’ก Concept: final, finally, and finalize

The keywords and constructs final, finally, and finalize serve very different purposes in Java despite their similar names.

๐Ÿ“˜ Quick Intro

final is a keyword used to declare constants and prevent method overriding or inheritance; finally is a block used to execute code after try-catch; finalize is a method called by the garbage collector before object destruction.

๐Ÿง  Analogy

Think of final as a locked door that cannot be changed, finally as a cleanup crew that always finishes its work, and finalize as the farewell message before demolition.

๐Ÿ”ง Technical Explanation

  • final: Prevents variable reassignment, method overriding, or inheritance.
  • finally: Executes code after try-catch regardless of exceptions.
  • finalize(): Called by GC before reclaiming object memory (deprecated in recent Java).
  • final applies to variables, methods, and classes.
  • finally block ensures resource cleanup.

๐ŸŽฏ Use Cases

  • โœ… Use final for constants and immutable references.
  • โœ… Use finally to close resources like files and streams.
  • โœ… Avoid relying on finalize(); use try-with-resources and explicit cleanup.

๐Ÿ’ป Java Code Example


public class Example {
    final int MAX_VALUE = 100; // constant

    void method() {
        try {
            System.out.println("Inside try");
        } catch (Exception e) {
            System.out.println("Inside catch");
        } finally {
            System.out.println("Inside finally");
        }
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("Finalize called before GC");
    }
}

โ“ Interview Q&A

Q1: What does final keyword do?
A: Prevents reassignment or inheritance.

Q2: When is finally block executed?
A: Always after try-catch.

Q3: What is finalize method?
A: Called by GC before object destruction.

Q4: Can final variables be changed?
A: No.

Q5: Is finally mandatory?
A: No, but good for cleanup.

Q6: Can you override finalize?
A: Yes, but it's deprecated.

Q7: Does finally block execute on System.exit()?
A: No.

Q8: Can final methods be overridden?
A: No.

Q9: Difference between final and finally?
A: final is keyword, finally is block.

Q10: Should you rely on finalize?
A: No, use explicit resource management.

๐Ÿ“ MCQs

Q1. What does final keyword do?

  • Allows reassignment
  • Prevents reassignment or inheritance
  • Overrides methods
  • None

Q2. When is finally block executed?

  • Only on exceptions
  • Only on no exceptions
  • Always after try-catch
  • Never

Q3. What is finalize method?

  • User method
  • Called by GC before object destruction
  • Static method
  • Constructor

Q4. Can final variables be changed?

  • Yes
  • No
  • Sometimes
  • Only in loops

Q5. Is finally mandatory?

  • Yes
  • No
  • Sometimes
  • Depends

Q6. Can you override finalize?

  • No
  • Yes, but deprecated
  • Always
  • Never

Q7. Does finally block execute on System.exit()?

  • Yes
  • No
  • Sometimes
  • Always

Q8. Can final methods be overridden?

  • Yes
  • No
  • Sometimes
  • Depends

Q9. Difference between final and finally?

  • Same
  • final is keyword, finally is block
  • Both blocks
  • None

Q10. Should you rely on finalize?

  • Yes
  • No, use explicit resource management
  • Maybe
  • Only for debugging

๐Ÿ’ก Bonus Insight

Understanding these three concepts avoids confusion and improves resource management and code safety in Java.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup