Can anybody explain me the following quote: "finalize() method can be invoked maximum once for any object."
thanks in advance.
No,is the answar. finalize() method is executed at the time when garbage collector is going to collect the object and once it happened the object is no more there,so the finalize() method can only called at once by the garbage collector.We dont even expect that block to be executed in a peculiar time,because garbage collection is dependant on JVM,so JVM has to decide it.
This is actually referring to the fact that the GC will only call the finalize method of an object, a maximum of one time -- it is not referring to how may times any other thread may call that method.
This may sound like a weird statement, but remember that it is possible for an object to be no longer eligible for garbage collection after the finalize method completes.
For example, during finalization, it is determined that the object should not be GC'ed. The method can set a reference to itself, in a collection, or a static variable somewhere, that will make itself reachable. It will no longer be eligible for garbage collection.
Care should be taken though, because if it ever becomes unreachable again, it will be simply garbage collected, as the finalize method will not be called again.
Henry [ September 09, 2006: Message edited by: Henry Wong ]