Hi Everybody, Consider the following program,originally created by Antti Barck
Here, getSimple() in combine class returns an object of Simple class, therefore according to me two references for Simple class object exists, (1)One in private object reference variable obj of CombineSimple (2)Second in bah a Simple class object reference variable in UseSimple's main method. If I set har=null, which is an object reference variable for CombineSimple,then a call to System.gc() invokes the finalize method for this object.Now my question is what happens to private variable obj of CombineSimple at the time of GC, since this variable and the variable bah in main both refer to same Simple object,will obj be set to null by the GCwhen it is forcefully called, thereby reducing the number of references to Simple object in heap, to one or will it do something else ? Please help.... THANKS
[This message has been edited by Bindesh Vijayan (edited August 29, 2001).] [This message has been edited by Bindesh Vijayan (edited August 30, 2001).]
Helo Bindesh! I guess Mr. Hockleys explanation on your other thread covered this one as well, so I am going to take five here... hey, Dallas, could you swing up something for this matter as well? ------------------ Antti Barck It Solutions Consultant -- NSD Oy Sun Certified Programmer for the Java� 2 Platform [This message has been edited by Antti Barck (edited August 30, 2001).]
Hello everybody Putting System.out.println(obj); within CombineSimple's finalize shows that obj is not set to null, at least until the execution of finalize. I think that setting to null the fields of a object pointed by reference that was set to null or becames out of scope, would be dangerous: other references could point to the same object. So, this could be done only when the gc determines that the object is not reachable. This doesn�t seem useful at all because the objects pointed by fields of an unreachable object are not going to be accessed via this object.
SCJP2. Please Indent your code using UBB Code
Joined: Aug 21, 2001
Thanks Jose you gave me a wonderful idea.I worked out and found that obj is never set to null even after the super.finalize() statement is called in finalize(),obj becomes unreachable as soon as har is set to null. [please correct if wrong] THANKS EVERYBODY
It seems that obj is unreachable after the closing bracket of the getSimple() method. har becomes unreachable after it is set to null and then it calls the finalize() method of CombineSimple. bah never becomes unreachable in this particular code, therefore the finalize() method of the Simple class is never called. If you set both har and bah to null, you will see both statements print out.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Hi, When you set har = null, the object referenced by har becomes eligible for GC.The System.gc() forces the finalize method to be called to allow GC.After the completion of finalize(), the har (along with its attributes obj) becomes eligible for GC. However, note that bah would still not be GCed, because the reference is still active.You will need to unreference the object held by bah to be allowed to GCed.This is possible if you set bah = null or reference bah to another object like bah = new Simple();. Hope this helps, Sandeep SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
Joined: Aug 21, 2001
Thanks Marilyn And Sandeep. As Sandeep say's After the completion of finalize(), the har (along with its attributes obj) becomes eligible for GC..But even after the finalize method is called,as Jose said,if you put System.out.println(obj); as the last line in finalize(),you will see Simple printed with some special characters.Does'nt this mean that obj is not set to null? But if you explicitly assign obj=null in finalize(),then it prints null. Please clear it.
Marilyn de Queiroz
Joined: Jul 22, 2000
If har = null, obj is not set to null. The reference to obj is set to null. All the links are broken and obj is eligible for gc even though obj itself is not null.