Jul 25, 2008

Finalization in Java

protected void finalize() throws Throwable

  1. It is declared in Object class.
  2. Is invoked by the garbage collector after it determines that this object is no longer reachable and its space is to be reclaimed.
  3. finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
  4. Subclasses of Object may override this definition.
  5. It is guaranteed that the thread that invokes finalize() will not be holding any user-visible synchronization locks when finalize() is invoked.
  6. Any exception thrown by the finalize() causes the finalization of this object to be halted, but is otherwise ignored.
  7. finalize() is invoked at most once per object, even if execution of this method causes the object to become reachable again and later it becomes unreachable again.