Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


Can you rely on finalize() method to be called as soon as an object is no longer referenced?

  1. Yes, it's guaranteed.

  2. No, it's not guaranteed.

  3. Only in Java 8 and earlier.

  4. Only if System.gc() is called.

The correct answer is: No, it's not guaranteed.

The finalize() method is used to perform any necessary cleanup actions for an object before it is destroyed by the garbage collector. While it was previously thought to be guaranteed to be called, this is no longer the case due to changes in how Java handles objects and memory management. In some cases, objects may be destroyed even if their finalize() method has not been called. Therefore, it is not safe to rely on this method to be called as soon as an object is no longer referenced. This makes option B the best choice. Options A, C, and D are incorrect because, as mentioned, the finalize() method is not guaranteed to be called and can no longer be relied upon in this way.