This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in finalize()" Watch "doubt in finalize()" New topic
Author

doubt in finalize()

Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
Hi All,

Can anybody explain me the following quote:
"finalize() method can be invoked maximum once for any object."

thanks in advance.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
That's wrong. The Gabage Collector will call finalize() maximum once (or never). Any other thread can call it as often as it wants to.

Yours,
Bu.


all events occur in real time
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
(correct me if im wrong)

GC will guarantee to call the finallize method once and only once on on that particular object which the GC attempts to delete.

but the GC it self is not guarantee to run... so the finalize method itself may never run.
[ September 08, 2006: Message edited by: Firman Drage ]

MV
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
Originally posted by Vaibhav Chauhan:
Hi All,

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in finalize()
 
Similar Threads
Reclaiming memory in java without using garbage collector........
can i write finalize mthd in an EJB??
finalize()
finalize() ?
JSP FAQ