The answer for this question is given to be option D in one of the mock exams. Could somebody explain why. I thought it was option B. 1.public static void main(String args[]) { 2.Button c = new Button("Smile"); 3.Button c1 = new Button("Frown"); 4.c1=c; 5. c=null; 6c1.setLabel("Laugh"); 7.Button c2 = new Button(); 8.} After execution of which statement number does the object originall held by variable c become eligible for garbage Collection ? A. 4 B. 5 C. 6 D. never
Deepak M
Ranch Hand
Joined: Jul 10, 2000
Posts: 124
posted
0
never in this method !
Harry Chawla
Ranch Hand
Joined: Jun 03, 2000
Posts: 97
posted
0
Think of c, c1 and c2 as reference variables that store the memory location of the object, i.e. a handle to the object. That means, the object originally held by variable c, viz Button("Smile") is now being referred by c1. c =null; means that variable c doesn't refer to anything now but proir statement c1 = c means that c1 holds the same memory reference as held by c at that time. So the answer here is never. I hope it helps...Thanks [This message has been edited by Harry Chawla (edited August 24, 2000).]
Vasanth Appaji
Greenhorn
Joined: Aug 22, 2000
Posts: 13
posted
0
What about the object that was referenced by c1 earlier before c1 was assigned c. Since the since object does not have any reference, it must be eligible for GC right???
Shankar G
Greenhorn
Joined: Sep 24, 2000
Posts: 16
posted
0
Yes, you are right -- the Button with the Label("Frown") would be eligible for GC. But the question was referring to the object referred by C. -- Shankar.
Originally posted by Vasanth Appaji: What about the object that was referenced by c1 earlier before c1 was assigned c. Since the since object does not have any reference, it must be eligible for GC right???
srikishore koduri
Greenhorn
Joined: Aug 29, 2000
Posts: 3
posted
0
I think C is right answer as the object is made into null and after that line it is never been used.