in the selftest of chapter9(SCJP 5), Question 16: (I cutted some code)
one answer here is:
B. the code will not compile because of an error in Line 7 of class Foo.
but the correct answer is
E. An exception occurs at runtime.
Now I am confused, because on page 719 of the same book it says:
wait() [...] must be called from within a synchronized content!
My prolem is that I remember having answered mock exams, whith exactly the same problem, and the correct answer was a compile time error. Of course I also checked the above answer by writing an own test and it doesn't give a compile time error.
But when in the exam, then in this case, I'd go for the runtime exception. Would be great to hear some comments, especially from people that already made the exam.
Many thanks in advance.
Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
Thanks for the post, I too was under the impression that lack of synchronization gives compile time error in case of wait.
Unfortunately, it is actually not possible to check for synchronization at compile time, especially for public methods. Since it is possible for code to synchronize an object externally, then call a method of that object, it is possible for the wait() method to work in that method, even though it doesn't synchronize internally.
I think there is a compile time error when wait()(also sleep(),join()) are called without the try/catch or Exception declaration.
For checked exceptions, it is possible to check at compile time. If the checked exception is to be caught by the caller then the method must be declared as throwing that exception.