About Question com.enthuware.ets.scjp.v6.1.796 :
Posted: Sat Apr 09, 2011 11:49 am
When I run the code below, I get these two answers.
It may print hello, hello, and world in any order.(I get this most of the time)
One possible output is: hello world(I get this sometimes)
The correct answer in the question is,
One possible output is: hello (I have never managed to get this output at all)
One possible output is: hello world
public class FinalizeTest {
private String value;
public FinalizeTest(String text) {
this.value = text;
}
public static void main(String[] args) throws Throwable{
FinalizeTest f1 = new FinalizeTest("hello");
FinalizeTest f2 = new FinalizeTest("world");
f1.finalize();
f1 = null; f2 = null;
System.gc();
}
public void finalize() throws Throwable{
System.out.println(this.value);
super.finalize();
}
}
Thank You.
It may print hello, hello, and world in any order.(I get this most of the time)
One possible output is: hello world(I get this sometimes)
The correct answer in the question is,
One possible output is: hello (I have never managed to get this output at all)
One possible output is: hello world
public class FinalizeTest {
private String value;
public FinalizeTest(String text) {
this.value = text;
}
public static void main(String[] args) throws Throwable{
FinalizeTest f1 = new FinalizeTest("hello");
FinalizeTest f2 = new FinalizeTest("world");
f1.finalize();
f1 = null; f2 = null;
System.gc();
}
public void finalize() throws Throwable{
System.out.println(this.value);
super.finalize();
}
}
Thank You.