Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in finalize()

 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taken from MasterExam:

I thought that finalize() can only be called once for an object, and since the program is already calling finalize(), gc will not call it.
Please explain.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The finalize method is never invoked more than once by a Java virtual machine for any given object. In for loop you are yourself invoking the finalize method programmatically and not the JVM automatically ( which is done when objects are GCed).

Moreover, the finalize method is not guaranteed to be called by the GC. The only guarantee is that if called once by JVM it won't be called again by JVM at the time of garbage collection.
[ December 14, 2008: Message edited by: Harvinder Thakur ]
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Harvinder.
I thought that finalize() can be invoked only once for an object(program or JVM).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic