Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

garbage collection

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never in this method !
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic