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 ?
Overview. Finalize method in Java is an Object Class method that is used to perform cleanup activity before destroying any object. It is called by Garbage collector before destroying the object from memory. Finalize() method is called by default for every object before its deletion.
The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the object is destroyed. The method is protected and therefore is accessible only through this class or through a derived class.
The finalize method is called when an object is about to get garbage collected. That can be at any time after it has become eligible for garbage collection. Note that it's entirely possible that an object never gets garbage collected (and thus finalize is never called).
It is invoked only once during the execution of a program. Following are some notable points about the finalize method. Since this method belongs the Object class, which is the super class of all the classes in java, you can override it from any class.
The order is different:
See http://java.dzone.com/articles/ocajp-7-object-lifecycle-java
Object lifecycle:
- Created
- In use (strongly reachable)
- Invisible
- Unreachable
- Collected
- Finalized
- Deallocated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With