aspose file tools*
The moose likes Beginning Java and the fly likes finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finalize method" Watch "finalize method" New topic
Author

finalize method

Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
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



Suman Mummaneni
Bangalore
India
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
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

Joined: Oct 13, 2005
Posts: 38910
    
  23
Posted by Suman Mummaneni
suggestions are welcome
Try it, and tell us what happens.
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
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
 
 
subject: finalize method