hii in one question dan
class I {
private
String name;
protected void finalize() {System.out.print(name);}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = null;
}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
System.gc();
}}
After method m1 returns, the object created on which line is not eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above
f. Compile-time error
g. Run-time error
the answer is "a"
with expanation...
Please note that this question asks which objects are NOT eligible for garbage collection after method m1 returns. After method m1 returns, the array a1 created on line 1 is not eligible for garbage collection. Method m1 sets all elements of the array to null; so the objects created on lines 2, 3 and 4 are eligible for garbage collection when method m1 returns.
i didn't understand why the elements of a[0], a[1], a[2] become null...when there refernce in main method is still there ?