i've been really bothered by these to exercises (10 and 11 from the book's third chapter), i just can't get it.
The answer is that 5 objets were created and 2 are elegible for GC, being "the second Dozens object (and its "int array" object" the ones that are not reachable. But instantiating a Dozens object does not create the int array, that's just an instance variable. Im i right? I know i must be wrong, but this is bothering me, i can't figure out why the int array counts as a reachable object here.
And the next exercise
In this case, there is only 1 object elegible for GC, because "there is still a reference to a2 (sure) and a2.b2 (sure!)", but here is the thing, "What might be less clear is that you can stil access the other Beta object through the static variable a2.b1 -because it's static". So a2.b1 it's static, but what does it have to do with a1 and b1, they where both set to null!
Sorry to bother, it seems i can't quite grasp this subject, if someone can give me a hand or at least point me to a good garbage collection tutorial i will be really thankfull.
An object becomes eligible for GC if it is not referenced by any reference variable.
Q.10 you have written incomplete, there is one more line:
da = null;
See, each Dozen object has a reference variable to an int array object containing elements 1,2,3.....so on
Thus when you create a Dozen object, an int array object containing elements 1,2,3...so on also get created and the reference to that array object lies in the dozen object as 'dz'.
Had the definition of class Dozens would have been something like this
then when you create a dozen object, no array object would have been created, ofcourse there would be an int array reference in each Dozen object but it would have been initialized to null(Default value of object-type instance variable).
So, now i hope you might have understood how 5 objects were created.
Now as da = null and d = null
the second Dozen object becomes unreachable, and as it becomes unreachable, the array object pointed by reference variable inside it also becomes unreachable....
Thus 2 objects become unreachable and thus eligible for GC
See, imagine a class like this:
Now when i create an object of this class,
UnderstandStatic u1 = new UnderstandStatic();
it has a primitive variable num with value 0(default value),
when i create another object of this class
UnderstandStatic u2 = new UnderstandStatic();
and write something like:
u2.num = 5;
Now when i print u1.num, what do you think would get printed?
5 will get printed.
A static variable is a class variable. It is shared by all objects of the class.
Now coming back to your question,
when we have written
a1.b1 = b1;
we tell Class Variable b1 to point to that b1 object.
Now a2.b1 would also point to b1 object.
More precisely there is no a1.b1 and a2.b1, there is just an object reference variable of type Beta named as b1 and it is pointed to a Beta Object.
Thus a1.b1 and a2.b1 point to the same object.
Now, even if we can't access that object by b1 and a1.b1, we can still refer it by a2.b1, thus the object is reachable and thus not eligible for GC,,,
Hope you might have understood.......
Waiting for enlightenment....
Joined: Jun 22, 2010
You were very clear in your explanation in both exercises,
so i understand both issues now,