class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3; //6
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}
After line 6, how many objects are eligible for garbage collection?
I got this question from examsguide.com free sample questions...
please help
first an objectod C is created which is referenced by c1.
then c1 is passed into method m() which does nothing but creates another C object that is referenced by c2
and finally a thierd object referenced by c3 is created.
when line c2=c3 is done..
it simply makes c2 refer to the object also referenced by c3..
so there is no reference left to the object that was originally referenced by c2.
so only 1 object is eligible for GC.
hope it helps...
ankur tyagi wrote:first an objectod C is created which is referenced by c1.
then c1 is passed into method m() which does nothing but creates another C object that is referenced by c2
and finally a thierd object referenced by c3 is created.
when line c2=c3 is done..
it simply makes c2 refer to the object also referenced by c3..
so there is no reference left to the object that was originally referenced by c2.
so only 1 object is eligible for GC.
hope it helps...
I dont think your answere is correct. After line#6 there are no objects eligible for garbage collection. Ref. variables c1,c2,c3 are still refrencing C object.
I dont think your answere is correct. After line#6 there are no objects eligible for garbage collection. Ref. variables c1,c2,c3 are still refrencing C object.
Well there is 1 object eligible for GC. The explanation is correct. c1,c2 and c3 do refer to an object of class C, but the object that c2 pointed to originally will be eligible for GC...