Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 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

[about gc]..

 
Ranch Hand
Posts: 78
  • 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:
1.public class Test {
2.public static void main (String args []) {
3.Employee e = new Employee("Bob", 48);
4.e.calculatePay();
5.System.out.println(e.printDetails());
6.e = null;
7.e = new Employee("Denise", 36);
8.e.calculatePay();
9.System.out.println(e.printDetails());
10.}
11.}

A.Line 10
B.Line 11
C.Line 7
D.Line 8
E.Never
The answer is C,can somebody tell me why??
----------------------------------------
thanks for help
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After e is set to null at Line No 6, it is eligible for garbage collection.
Hence at the end of Line 6, or before Line 7 it can be GCed.
HTH
tvs sundaram
 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But at line 7,reference e is used again,
so why line 7 can cause garbage collection??
---------------------------------
thanks for help.
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
As far as I can see the object orginally held are the important words here because a new object is created in line 7 and assigned to the variable e but the old object is not used again.
Hope that helps,
Terry
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the earliest time should be line 6. But since the answer does not provide 6. Line 7 becomes the answer.
Guoqiao

Originally posted by chao-long liao:


 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic