aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() 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()" Watch "finalize()" New topic
Author

finalize()

Rakesh Ne
Greenhorn

Joined: Mar 12, 2007
Posts: 14
If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection
1. The compiler will throw an error.
2. The garbage collector "collects" the object anyway.
3. The garbage collector does not collect the object in the present sweep. But when the object becomes eligible for garbage collection again, its finalize method will not be called by the garbage collector (i.e., if the garbage collector gets a chance to run.). It will simply be garbage collected.
4. The object can never be garbage collected and hence leads to memory-leak. Each time the garbage collector calls finalize before "collecting" the object the object "resurrects" itself.

The answer for the above question is given as 3. I believe it is 4. Can somebody please explain why is it 3.

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19071
    
  40

Rakesh,

Please quote your sources. Thanks.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rakesh Ne
Greenhorn

Joined: Mar 12, 2007
Posts: 14
Originally posted by Henry Wong:
Rakesh,

Please quote your sources. Thanks.

Henry


Source : JavaBeat
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Rakesh,

your posts involve designers issues.
Just we need to know rules optional to find "why those rules" for certification.

There are so many other "whys" we need to learn as developers.
Please refer to JLS if you found any conflicts as per design of language.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi Rakesh,

You ask a valid question that is definitely in the scope of the SCJP exam!

Can you explain your understanding of the rules of the finalize() method and what you think is either true or false about options 3 and 4?

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Rakesh Ne
Greenhorn

Joined: Mar 12, 2007
Posts: 14
I was under the impression that finalize will be called always when an object is being garbage collected. After looking at Srinivasan's post, I have done some reading about Garbage Collection and finalize in particular. I realized that when an object comes for garbage collection for the second time, JVM actually remembers that and goes ahead and deletes that object from heap. And that clears all my confusion over option 3 or 4.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize()