• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B Chapter 3, Self-Test Question 12

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question here is, At which point is only one object eligible for garbage collection?

class Eco{
public static void main(String[] args){
Eco e1 = new Eco();
Eco e2 = new Eco();
Eco e3 = new Eco();
e3.e = e2;
e1.e = e3;
e2 = null;
e3 = null; //line 9
e2.e = e1; //here NullPointerException
e1 = null;
}
Eco e;
}



I am fine with the NullPointerException, but I also chose the answer "after line 9 runs". The answer in the test makes it clear that the question creators intended to create a sort of island where all three objects are chained to each other.
However the way the code is presented, the island is not complete (e2 is not successfully chained to e1). If there is no chain to e1, however, I thought that at least e2 should be eligible for garbage collection as soon as e3 is nullified (and with it the last pointer to e2). Is this wrong thinking?
As I say, I know the code would not run to the end, but "eligible for gc" in my opinion meant "no matter whether the program stops at some point".
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After line 7 the object structure is like this



after line 9, the object structure is like this



So as you can see, the object actually referenced by e3, still has a reference through e1.e....
[ August 31, 2008: Message edited by: Ankit Garg ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic