This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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?
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Joined: Mar 12, 2007
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.