• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Q.31 from John Hunt

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:

public class Test {
public static void main (String args []) {
Employee e = new Employee("Bob", 48);
e.calculatePay();
System.out.println(e.printDetails());
e = null;
e = new Employee("Denise", 36);
e.calculatePay();
System.out.println(e.printDetails());
}
}

A.Line 10
B.Line 11
C.Line 7
D.Line 8
E.Never
Select the most appropriate answer.
The correct answer to this question is C which is line 7 but I thought it was line 6, because when you say e=null it is ready to be garbage collected???

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is line 7 because in line 6 the assignment takes place "e = null;" and when this is completed, you are already in line 7. I would also presume that the answer would be line 7 even if we do not have the next assignment statement for e in line 7.
Feel free to correct me if my presumptions are incorrect....
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds right. The line makes the old object unreachable. After that it is collected when ever the GC gets around to it.
And Sudhir Bangera is right, whats on line 7 really doesn't effect the when it could be GCed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic