A. After line 8 runs. B. After line 9 runs. C. After line 10 runs. D. After line 11 runs. E. Compilation fails. F. Never in this program. G. An exception is thrown at runtime. Answer I feel that the answer should be B. After line runs isn't e3 eligible for garbage collection. As e3 is set to null at line 9 and at line 6 e3.e =null, so e 3 should be eligible after line 9. Can somebody explain. Thanks.
In this type of question, the safest way to do it with a pencil and paper and draw out the whole thing instead doing it in the head.
I don't know if you mistype something, but you said "at line 6 e3.e =null" but I see line 6 as e3.e = e2;
The answer is G. An exception is thrown at runtime is because of line 8 e2 = null; Once you set any object to null, you can't no longer tough any of it's field. In this case e2.e. That causes the NullPointerException to be thrown.
e2====>object===>e but once you do e2=null;
all you get is e2 object===>e You can't no loger get to that object.
At what point is only a single object eligible for GC? Hi,I forgot to modify the code earlier. Please look at the modified code I have moved before we set e2 to null. In the K&B book answers sections it says that with the code above the answer would be
Never in this program
. But I think after line number 10, e3 should be eligible for garbage collection. Please clarify.