• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Garbage collector

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}

Line 10
Line 11
Line 7
Line 8
Never
Answer is line7 but I don't understand why? Can somebody explain me?
Thanks,
Ash
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 3 ,the Employee object 'e' is assigned a reference to the object created as new Employee("Bob",48);
At line 6 e is assigned a null reference ,it no longer refers to the object it was earlier referring to.
At line 7 it is assigned a reference to a new Employee Object("Denise",36).
There are no more references to the earlier Employee object ("Bob",48). Hence it may be garbage collected
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic