garbage collection, java, static

Question Description

Hi, I am going through SCJP material and I have a doubt regarding a question I came across in that material:

3. class Beta { }
4. class Alpha {
5. static Beta b1;
6. Beta b2;
7. }
8. public class Tester {
9. public static void main(String[] args) {
10. Beta b1 = new Beta(); Beta b2 = new Beta();
11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
12. a1.b1 = b1;
13. a1.b2 = b1;
14. a2.b2 = b2;
15. a1 = null; b1 = null; b2 = null;
16. // do stuff
17. }
18. }
When line 16 is reached, how many objects will be eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
® ✓ B is correct. It should be clear that there is still a reference to the object referred to by
a2, and that there is still a reference to the object referred to by a2.b2. What might be
less clear is that you can still access the other Beta object through the static variable
a2.b1—because it's static.

For this question I thought the answer would be 4 at first, since a1 is declared NULL. This a1 and a2 has two other objects b1 and b2, which are made to point to the other two objects b1 and b2. But they are also declared NULL, so I thought a1, a1's b1, b2, a2's b2 will also become NULL.

Please help me understand this answer B.


Final Answer

The use of the new keyword ensures that objects are constructed and have an address.  All 4 of those object, a1,a2,b1,b2 constructed in the main(..) method are local to that method. 

When you

12. a1.b1 = b1;

That sets a1's static beta field to a real address.   Later in main(..) when you set b1=null it null's out the address of that local Beta object but does not change the address that a1.b1 has already been set to.


15. a1 = null; b1 = null; b2 = null;

Deletes the a1 object and, of course, it's a1.b1.  The a2 object is still there and it's static field a2.b2 still has it's value.  Thus at line 16 there is only the a2 object with it's static field a2.b2 that still has a valid address, ie, reference.

>> Hmm, I decided to review the definition of 'eligible for garbage collection':

An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.
So, I've decided that my answer is not as helpful as I thought it would be.  Rather than delete it I'm leaving it because it might be helpful, don't pay for it though..

Boston College

Return customer, been using sp for a good two years now.

Thanks as always for the good work!

Excellent job


Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors