File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

antraarora
Ranch Hand

Joined: Jul 27, 2000
Posts: 45
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
never in this method !
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
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
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
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
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
 
subject: garbage collection
 
Similar Threads
Doubt about garbage collection
Problem regarding garbage collection
Garbage collection?
Garbage Collection Qstn from K&B Doubt