Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

How this works here

public class Gabc{


static int i=0;

Gabc g=null;


public void finalize(){

i++;

}


public static void main(String arhs[]){

Gabc gab1=new Gabc();

Gabc gab2=new Gabc();

Gabc gab3=new Gabc();

gab3.g=gab1;

gab1.g=gab2;

gab3.g=gab2;

gab1=null;

gab3=null;

Runtime.getRuntime().gc();

System.out.print(i+" objects removed");

}


}

The question is to predict the outcome of the program. However, you can see the output depends on the execution of the finalize() method, which happens only when the garbage collector actually removes the object.


The only thing we can say for certain is whether an object is eligible for the garbage collector or not. Being eligible for the garbage collector doesn't mean the garbage collector will remove the object. So the execution of the finalize() method is not predictable.

ExamLab © 2008 - 2024