• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

doubt in finalize()

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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

thanks in advance.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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 ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic