File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
antraarora
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never in this method !
 
Harry Chawla
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think C is right answer as the object is made into null and after that line it is never been used.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic