Results 1 to 5 of 5
  1. #1
    shantharam is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default garbage collection

    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,

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: garbage collection

    Before b1 and b2 are set to null a1.b2 and a2.b2 are "pointed" at them. Since there are still references to these objects they cannot be eligible for GC.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: garbage collection

    I got it junky but a2 is pointing to null ??? how is it ? then whats the difference between pointing to null and equal to null

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: garbage collection

    a2 is never made to point to null, only a1 is. As for b1 and b2, only the references in the main method is made null; the objects themselves can be reached through a2.b1 and a2.b2. The a2.b1 threw me for a loop first, but since it's declared static, when you set a1.b1, it automatically set a2.b1 as well, since they're the same class (and static is shared between classes, not instances).

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

Similar Threads

  1. Weird Garbage Collection...
    By fatabass in forum New To Java
    Replies: 16
    Last Post: 12-20-2011, 09:59 PM
  2. Garbage Collection Pauses
    By jdpl28 in forum Advanced Java
    Replies: 0
    Last Post: 07-16-2011, 02:28 PM
  3. i m so confuse in garbage collection
    By vivek6569 in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 07:39 AM
  4. JVM Garbage Collection
    By daromnet in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2011, 10:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •