This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize method

 
Suman Mummaneni
Ranch Hand
Posts: 92
Google App Engine Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
What happens if I override the finalize method present in the Object class and create a new instance of the object in the finalize method. Sample code given below

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As written, you create a new instance of MyClass, which goes out of scope and is available for garbage collection when the finalize method exits.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a cunning, but pointless, infinite loop, isn't it?

finalize() is a poor mechanism, with few real uses. Not much point getting to involved in it.
 
Suman Mummaneni
Ranch Hand
Posts: 92
Google App Engine Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still do not understand the use of this method. If you creating a new object inside the finalize. This object will be available for GC again in this new object GC will call the finalize method which creates another new object. But this goes on util the JVM will exist. So basically the GC is going in loops. for removing one single object. This is my conclusion from this code.

I do not know if this is correct or not. Comments and suggestions are welcome.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted by Suman Mummaneni
suggestions are welcome
Try it, and tell us what happens.
 
Suman Mummaneni
Ranch Hand
Posts: 92
Google App Engine Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well guys here is what happens.
Nothing will happen.
The GC will not go in a loop nor the JVM will crash. As per the rules of GC the program will exit normally.
Here is the link from Java Forums by Sun. that explains it clearly

http://forum.java.sun.com/thread.jspa?threadID=5181278&tstart=15
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic