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

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

Prepare for the Java Mastery Quiz with engaging questions and essential hints designed to enhance your understanding of 'Thinking in Java'. Get ready to solidify your programming skills!

Practice this question and more.


Which method does Java automatically call on an object before garbage collection?

  1. clean()

  2. finalize()

  3. delete()

  4. remove()

The correct answer is: finalize()

Java has a feature called garbage collection which automatically frees up any memory that is no longer being used by an object. Before destroying an object, Java automatically calls its finalize() method to perform any cleanup tasks, such as closing open files or releasing resources. Therefore, the incorrect options are A clean(), C: delete(), and D: remove() as they are not methods specifically used for garbage collection in Java.