Java, Life cycle of Object.

  java, question

Today, I suddenly recall a previous interview question, how to obtain the existence time of an object, that is, from creation to destruction, the object is recycled under GC control, so is there any way to obtain the destruction time?

All objects have one

protected void finalize();

Methods. FromObjectFrom the. This method is called before the object is recycled. You can rewrite this method and print the current time in it.

As for the creation time, it can be printed in the construction method.