Originally posted by dhwani mathur:
can anyone explain me in which line the object obj1 is elligible for garbage collection?
After line 5. Let us look at this step by step.
Line 1: A new object is created (say Object1), and the reference variable obj1 points to it.
Line 2: A new object is created (say Object2), and the reference variable obj2 points to it.
Line 3: We assign a copy of the reference variable obj1 to the reference variable obj2. After this step, both variables points to Object1. Object2 is eligible for garbage collection.
Line 4: We set the reference variable obj1 to null. After this step, there is still the variable obj2 that points to Object1.
Line 5: We set the reference variable obj2 to null. After this step, there are no more references to Object1. Object1 is eligible for garbage collection.