Bird
0
0

Why is relying on the finalize() method for resource cleanup considered a bad practice in Java?

hard📝 Conceptual Q10 of 15
Java - Classes and Objects
Why is relying on the finalize() method for resource cleanup considered a bad practice in Java?
ABecause <code>finalize()</code> is called multiple times per object
BBecause <code>finalize()</code> runs immediately after object creation
CBecause <code>finalize()</code> prevents garbage collection
DBecause <code>finalize()</code> execution timing is unpredictable and may never run
Step-by-Step Solution
Solution:
  1. Step 1: Understand finalize() behavior

    The finalize() method is called by the garbage collector but its timing is unpredictable and may never be called.
  2. Step 2: Clarify misconceptions

    It does not run immediately after creation, does not prevent GC, and is called at most once per object.
  3. Final Answer:

    Because finalize() execution timing is unpredictable and may never run -> Option D
  4. Quick Check:

    finalize() timing unpredictable = A [OK]
Quick Trick: finalize() is unreliable for cleanup, avoid using it [OK]
Common Mistakes:
  • Thinking finalize() runs immediately after creation
  • Believing finalize() stops garbage collection
  • Assuming finalize() runs multiple times

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes