File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finalize Method

 
ms
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
1) If in finalize method we make re refrence to the object .Will that object ever be garbage collected.
2) Is it mandatory to call the base class finalize method in the subclass's finalize method else the compiler will give compile time error .

 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ms , welcome to javaranch.
PROPER NAMES ARE NOW REQUIRED

Please Refer to the official naming policy of javaranch. You will have to reregister yourself with proper first & last name & help maintain the decorum of the forum.
Waiting for your new posts with proper first & last name

Your Friendly Bartender
Shailesh.
 
vadiraj vd
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ms !!

1) If in finalize method we make re refrence to the object .Will that object ever be garbage collected.

No. since the gc calls the finalize method only once. If you make any reference to an object then it will not be GC'ed.

2) Is it mandatory to call the base class finalize method in the subclass's finalize method else the compiler will give compile time error .

Compiler won't give any error, but it's neccessory to call the superclass's finalize as finalize methods are not chained by default.
I hope this is clear.
Regards,
Vadiraj. scjp

 
ms
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Vaidraj ,
Actually I read these two questions in one mock test and it was said that that first time the object will not be garbage collected but second time the GC will ignore finalize and the object will be garbage collected .I could not understand the logic .
 
Naveen Sharma
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ms You it is true that finalize() will called only once on an object.
If your creating a new Instance by using this() in finalize(),that means you r again creating the instance of this type but second time finalize() won't be called.
And also if there is not any refrence which is pointing to the newely object created , it will be again grabage collected.So All the objects which r not represented by any variables are eligible for GC
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic