• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize()

 
Rakesh Ne
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh,

Please quote your sources. Thanks.

Henry
 
Rakesh Ne
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Rakesh,

Please quote your sources. Thanks.

Henry


Source : JavaBeat
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rakesh Ne
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic