I am not able to guess the correct answer for a question which asks about the number of objects that will be applicable for garbage collection after a particular line is executed. Please can somebody guide me for that.
A lot of people have problem with this. Search the forum and you'll find a lof of results which might be helpful. You might choose any strategy that people use. Just go through a few of the posts and you might find a strategy for these questions useful...
Well I tried to understand that by reviewing those questions however I don't understand the main logic behind that. I have gone through the K&B book once related to that topic but could not make it out.
Sorry Ankit Garg but only one object will be garbage collected and that's the object that a1 was pointing to.
With regard to the B object with string s ref in it, that object was pointed to by b (from the stack) and it is still referred to by b which I didn't see in your diagram. The following statement : a1.obj = b; all it does is just copies the reference pattern of b to a1.obj but b is still pointing to B object with s string instance variable, thus only one object is collected.
Thanks for the diagrams but I'm having a little trouble understanding exactly how to build these - especially when the instances inside of a class are another class.
Question, in your example how many objects are ready for GC? 3?
When it says something like A a1 = new A(); even if A has instances of B or C, those don't have objects until they are set to something or a constructor is used? I was using ExamLab and I'm not entirely sure why I got the diagramming wrong on this example:
If someone could help me out it'd be greatly appreciated.
Well its the same as passing an object reference to a method. Here you will be passing the reference to the constructor. In the constructor, for that instance it can be given to an instance reference. If you clear this thing in your concept, rest of the thing will fall into place. Also what object is eligible for gc is the one which is not referenced from a live thread. Also creating object, go from right to left i.e first evaluate the new and then assign that to a reference. Use the same analogy for multiple objects in the same line.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
A timing clock, fuse wire, high explosives and a tiny ad: