garbage collection, java, static

User Generated

funagunenz

Programming

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
Answer:
® ✓ 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.

Thanks,

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & 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.

This

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. 

http://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
<<<
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..






Anonymous
This is great! Exactly what I wanted.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags