javagarbage-collectionfinalize

Java Interview Question: finalize() method


I was given the following phrase in an interview:

The invocation of an Object's finalize() method is the last thing that happens before an object is garbaged collected.

I had to answer by:

I've chosen True but it was wrong. Can you please explain me why ?


Solution

  • The order is different:

    1. First the object is collected.
    2. Then the object is finalized.

    See http://java.dzone.com/articles/ocajp-7-object-lifecycle-java

    Object lifecycle:

    1. Created
    2. In use (strongly reachable)
    3. Invisible
    4. Unreachable
    5. Collected
    6. Finalized
    7. Deallocated