Hi,
Can anyone pls help. How many objects are eligible for GC when System.gc() is invoked() in the following code:
class MyClass{
public void finalize()
{System.out.println("finalize");}
class Test{
private static void f (MyClass[] array){
array[0]=null; //line 1
array[1]=array[0]; //line 2
array[2]=array[1]; //line 3
array=null; /line 4
}
public static void main(String agrs[]){
MyClass[] array = new MyClass[4];
for (int i=0; i<array.length;i++)
array[i]=new MyClass();
f(array);
System.gc();
}
}//end class
correct answer is 3 whereas i feel it should be 4( the objects at line 1,2,3 and 4). In the method f() 3 out of 4 elements are made null and the array refernce itself is made null (at line4) then how come only 3 objects are eligible for GC.
thanks,
Sarika.