This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
we are passing the refrence copy of c2. c2 is a object reference variable. So both 'cb' and 'c2 ' wpointed to same object refernce only know. hence when 'cb' is set to null,the 'c2' will also be 'null'
Thats the reason i think c2 will also become 'null'.
Please explain with an example.
Joined: Jan 16, 2008
Correct, both 'cb' and 'c2' point to the same object.
So, what happens when 'cb' is set to null?
It stops pointing the object and points to null. This does not affect 'c2' at all! This does not affect the real object also!
Example: Assume object1, object2, ..., object100 of type Object that all reference the same object on the heap.
If one of them is set to null,
all the others still reference the same live object!
cb and c3 are just references but not objects. Lets see the stack and heap here.
stack|heap c1 | cardboardobj1,storyobj1 c2 | cardboardobj2,storyobj2 c3 is reference variable. Now cb ref vble refers to c2. when cb = null, cb ref vble is made null but c2 object is still intact. c3 ref vble is also null. But remember ref vbles live on the stack and only objects live on the heap. so cb and c3 are not on heap at all. Now when c1 = null, the 2 objects associted with c1 are available for garbage collection as no references are referring to them anymore. so 2 objects are available for GC
If you worry you cannot work... If you work you need not worry