When new Phoenix() will call finalize() method, finalize() will call History.send(this); here, whatever object will call finalize(), its reference will be passed to static static void send(Phoenix p), and History class will store it as static Phoenix p2;
so static Phoenix p2=firstPhoenix
same thing will happen that happened with firstPhoenix and after complete execution
static Phoenix p2=secondPhoenix;
Now firstPhoenix reference is not held by any reference variable, so it is elligible for garbage collection.
But secondPhoenix is not eligible for gc as its reference is held by static phoenix p2 reference.
So only one object is eligible for garbage collection. so option d.
The JVM could have invoked the finalize() method at least once.
while the code reoresented by"// do time consuming and memory intensive stuff" is executing, which are true concerning the three Phoenix objects we know have been created?
SCJP 6
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
Wolfgang Schwendt wrote:Can anyone explain to me why answer 5 is false?
Initially, only the first Phoenix object is eligible for GC. But once the JVM invoked finalize() on the first Phoenix object, this object gets resurrected and becomes uneligible for GC, while now the second Phoenix object has become eligible for GC.
Therefore, it's possible that the JVM also invokes finalize() on the second Phoenix object. Thus, in _total_ it's possible that we get two finalize() calls by the JVM (of course the JVM invokes finalize() only at most once for each object).
So how come answer 5 false?
Henry Wong wrote:
Wolfgang Schwendt wrote:Can anyone explain to me why answer 5 is false?
Initially, only the first Phoenix object is eligible for GC. But once the JVM invoked finalize() on the first Phoenix object, this object gets resurrected and becomes uneligible for GC, while now the second Phoenix object has become eligible for GC.
Therefore, it's possible that the JVM also invokes finalize() on the second Phoenix object. Thus, in _total_ it's possible that we get two finalize() calls by the JVM (of course the JVM invokes finalize() only at most once for each object).
So how come answer 5 false?
But doesn't choice 5 say more than once for "three" objects? You only described two of the objects. How does the third object gets it's finalize method called more than once?
Henry
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:
It actually says "more than once total" for the 3 objects. I read the "total" as in combining the number of times that finalize gets called for all three objects. In any case, if that's not what it means, the question could be worded more clearly.
Henry Wong wrote:
But doesn't choice 5 say more than once for "three" objects? You only described two of the objects. How does the third object gets it's finalize method called more than once?
Wolfgang Schwendt wrote:
Thanks,
it could not only be worded more clearly, it should...
I for one at least, as a non-native speaker of English, had difficulties with the wording "more than once total for these three objects". I interpreted it in the way that it refers to the number of times, finalize() might have been called for all objects (of course, at most once for a single object). And my understanding, as described above, is that 2 invocations of finalize() by the JVM are possible (albeit not guaranteed).
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:
I honestly think it's a mistake, and Answer 5 should be correct as well.
while the code reoresented by"// do time consuming and memory intensive stuff" is executing, which are true concerning the three Phoenix objects we know have been created?
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:Hi Punit,
So the first object is eligible for garbage collection. This means that while the time-consuming code is executing, the JVM might call the finalize() method on that first object. This will cause the first object to be referred to from History, and the second object in turn to be eligible for GC. Then, the JVM might call finalize on the second object, which will become referenced by History, and the first object will again become available for GC. Am I missing something?
Thanks.
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Bert Bates wrote:Hi Guys,
It might be that there is an error here but I'd like to ask you to summarize your concern - please post the entire question with the summary of your argument - thanks! When you post the question please don't change ANY of the code or the wording - remember, not everyone has a copy of the book, and if you change even a single word of the original question it might make a change that you're not expecting - just like real code![]()
Also, don't worry, the questions in the book or on the book's master exam CD or on the McGraw Hill website are TOTALLY DIFFERENT than the questions on the real Sun exam. There is no overlap, I promise![]()
hth,
Bert
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:
Wolfgang, can you take care of gathering the information? Let's make sure the actual question wasn't changed in any way, because as Bert says a simple word change can make a big difference.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Bert Bates wrote:Okay, I'll probably regret being dense here, but just to confirm:
Is it clear to everyone the distinction between when the programmer explicitly invokes finalize() vs. when the JVM chooses to call finalize()?
Wolfgang Schwendt wrote:
Done.
https://coderanch.com/t/429597/Mock-Exam-Errata/certification/Error-SJCP-Learnkey-Bonus-exam#1907357
I also added the original question from the Learnkey exam.
Punit Singh wrote:Garbage Collector will run finalize() method of an object at most one time.
But here 2 different objects are taking part in the program, so question is asked about how many times GC has to call finalize() if needed.
You can call finalize() manually from your program, but that is different from when GC calls finalize() method during garbage collection, so if needed GC will call finalize() method of your object but at most once. But you can call finalize() method manually 100 times.... or more.
Calling manually finalize() is like calling run() method on Thread object instead of calling start(). You can call run() on a thread object any number of time you want, but you can call start() on a thread at most one time.
denis sorn wrote:
could you please explain why GC runs finalize() method?
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Bert Bates wrote:Dang! You guys got me!
![]()
This is the first time in a while I've had to break out the compiler to test a possible (now verified), errata!
So, now, who can explain why the 2nd Phoenix object can become GC-able?
All code in my posts, unless a source is explicitly mentioned, is my own.
Wolfgang Schwendt wrote:
denis sorn wrote:
could you please explain why GC runs finalize() method?
read and try to understand the contract for finalize()
http://java.sun.com/javase/6/docs/api/java/lang/Object.html#finalize()
The Javadocs contain a very good description.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |