So the answer is 5 objects created and 2 eligible for GC. Now you ask "why do you said 2 objects eligile for GC and spoken only about one Dozen object?
Have you ever heard about the "Island of Isolation"? Its an unreachable place for people outside the island, but inside the island thing happens. What do I mean? Look:
da = null
When you lose the reference to the Dozen object the int array (another object) is unreachable! It lives now in the Island of Isolation, the object itself still have a reference (the Dozen dz attribute) but since Dozen is unreachable you cant ever reach Dozen.dz!
Thats why you have 2 eligible objects for GC, not only the objects that have no reference become eligible, but all the objects that cant be reachable (living in the island of isolation).
So be carefull, when you find an object that has no reference, check if the object attributes doesnt refer to another objects that become unreachable too!
Sorry for the long post, hope it helps you understand what happen!
[ November 16, 2008: Message edited by: Fabio Nascimento ] [ November 16, 2008: Message edited by: Fabio Nascimento ]
The answer is 5 objects are created and 2 eligible for GC. But, my answer is 4 objects created and 1 for GC.
Maybe an option here is ... instead of everyone explaining why the answer is 5 objects created and 2 eligible for gc... it may be a good idea for you to explain why you think the answer is 4 objects created and 1 eligible for gc. This way we can figure out where you went wrong.
Statement #1: Dozens da=new Dozens;//object 2 Will create a reference name "da" in the main method stack and a new Array object in the Heap which can hold three objects of type Dozens. Hence this statement will have one object of type Array on the heap which is refered by da.
Statement #2: da=new Dozens();//object 3 : Will create a new Dozens object in heap and assigned to da. Before the Dozens() constructor runs the member variable of Dozens dz will result in a new Array object creation. Hence after this statement completes there will be two more objects in the heap , first of type Dozens assigned to da and a new array object assigned to dz memeber variable of da. Hence every new Dozens object will result in two new object on heap.
Hence there are a total of 3 objects in the heap.
Statement# 3: Dozens d=new Dozens();//object 4 Will result in two more objects as explained above.
Hence there are a total of 5 objects in the heap.
da=d; Will cause the second element of da array to refer the above object created by d.
Still there are a total of 5 objects in the heap.
d=null; Will cause the reference d to point to null but the object which it pointer earlier can still be refered from da . Hence Still there are a total of 5 objects in the heap.
Still none of the objects are eligible for garbage collection.
da=null; Will cause the object created by statement #3 no longer accessible from any live thread and hence that object will be available for GC. Since its a Dozens object it will cause dz also to be available for GC because although dz can be accessed via its object but the object itself cannot be accessed from live thread.
Hence a total of 5 objects are created and 2 objects are eligible for GC. Note "da" & "d" are local variables in the main method stack and not objects in HEAP.