What happens when finalize() is called on an object?
2017
What happens when finalize() is called on an object?
- A.
The object is immediately garbage collected.
- B.
The garbage collector calls it before collecting the object.
- C.
The object gets permanently deleted from memory.
- 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.