What happens when finalize() is called on an object?

2017

What happens when finalize() is called on an object?

  1. A.

    The object is immediately garbage collected.

  2. B.

    The garbage collector calls it before collecting the object.

  3. C.

    The object gets permanently deleted from memory.

  4. D.

    It prevents an object from being collected.

Attempted by 89 students.

Show answer & explanation

Correct answer: B

When finalize() is called on an object in Java, it is invoked by the garbage collector before reclaiming memory. This process is non-deterministic, meaning there is no guarantee when or if it will be called. It allows for cleanup operations prior to destruction, but relying on it for critical resource management is discouraged.

Explore the full course: Isro