This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finalize Method " Watch "Finalize Method " New topic
Author

Finalize Method

ms
Greenhorn

Joined: Oct 18, 2000
Posts: 10
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

Joined: Oct 12, 2000
Posts: 1874
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

Joined: Dec 15, 2000
Posts: 68
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


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
ms
Greenhorn

Joined: Oct 18, 2000
Posts: 10
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

Joined: Mar 23, 2001
Posts: 65
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
 
GeeCON Prague 2014
 
subject: Finalize Method