This is a prob from Tata mC graw Hill scjp & scjdJava 2 self guide pg452 12. X3 x2 = new X3(); //obj1 13. X3 x3 = new X3(); //obj2 14. X3 x5 = x3; 15. x3 = x2; 16. X3 x4 = x3; 17. x2 = null; 18. // insert code what two lines of code, inserted independently at line 18, will make an object eligible for garbage collection? (Choose two.) A. x3 = null; B. x4 = null; C. x5 = null; D. x3 = x4; E. x5 = x4; //answer given in book--->>.E and C
I agree with answer C but whats the use of assigning x4 to x5 in E.as C is sufficient to remove the last ref to obj 2.inserting statement E in the code would add one more reference var to obj 1.
Originally posted by karthik damodaran: Can anyone explain about that option x5=x4,
Will that be eligible for Garbage collection?
Yes, by the statement
what actually gets executed is, "the object being pointed by the reference variable x4 is now being assigned to the reference variable x5".
As such, the side effects are:
The object being pointed by x4 now has two references - x4 and x5 (newly added). Remember, still x4 is pointing to the same object until it is reassigned with some other object.
The previous object being pointed by x5 prior to the execution of this assignment statement has lost one of its active references (x5). Had x5 been the only reference to that object, now it is eligible for GC.