Page 1 of 1

About Question com.enthuware.ets.scjp.v6.1.796 :

Posted: Sat Apr 09, 2011 11:49 am
by ETS User
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.

Re: About Question com.enthuware.ets.scjp.v6.1.796 :

Posted: Sun Apr 10, 2011 5:29 pm
by admin
It is not necessary that all the outputs will be reproducible in all situations. A particular behavior may occur only in some situations. In this case, as the explanation notes, it is possible that System.gc() may not actually invoke the GC thread and that thread may not call the finalize() method of the objects at all. So in this case, only hello will be printed.

Re: About Question com.enthuware.ets.scjp.v6.1.796 :

Posted: Wed Apr 13, 2011 10:33 am
by Guest
Then all three options is theoretically possible, including the one below witch was not a part of the correct answer, sense GC might call finalize() even do you did it manually, correct?

It may print hello, hello, and world in any order.(I get this most of the time)