After method m1 returns, the object created on which line is eligible for garbage collection?
a. 1 b. 2 c. 3 d. 4 e. Compile-time error f. Run-time error g. None of the above
Answer: g (none of the above)..
Which of the following is a true statement?
a. The object created a line 1 is eligible for garbage collection after line 2. b. A NullPointerException is generated at line 3. c. The program compiles, runs and prints X. d. The program fails to compile. e. None of the above
Can anyone explain why a1 is not elligible for garbage collection in Question1?
And why "i" is not elligible in Question 2?
Thanks in advance..
(code tags added - please learn how to do this) [ May 19, 2004: Message edited by: Barry Gaunt ]
For your first question: remember that the reference a1 in the method m1 is a copy of the reference a1 created in the main method. Setting a1 to null in m1 does not set the a1 in main to null. Compare with passing a primitive value such as an int.
The same analysis holds for your second question. [ May 19, 2004: Message edited by: Barry Gaunt ]
In both the cases you are passing a reference to the methord m1. Inside method m1 there is a local reference a1 and i respectively. They are local to the methord m1 and does not have any relation with the instance reference. There reference get created inside the method m1 and loses its scope once the method finishes execution.
So if you make the local reference as null then only the local reference will not point to the required object. The instance reference continues to refer to the object.
The case is different if you try to change the value of the object. since the instance reference and local reference are pointing to the same object modification is possible to the object with both the reference.