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.
About Question com.enthuware.ets.scjp.v6.1.796 :
Moderator: admin
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.1.796 :
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.
If you like our products and services, please help us by posting your review here.
Re: About Question com.enthuware.ets.scjp.v6.1.796 :
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)
It may print hello, hello, and world in any order.(I get this most of the time)
Who is online
Users browsing this forum: No registered users and 43 guests