In the original code when you call wait, it is a call to wait on the this reference. Since the call to wait is not in synchronized context of this, so a IllegalMonitorStateException is thrown. Since the associated catch block does nothing when an exception is raised, so you see no output.
You are right that calling s.wait(5); will solve the problem as the code is synchronized on s. But that is not a perfect solution. The call to wait will return only because of timeout i.e. the 5 milliseconds will expire. The call will not return if you write s.wait() as no one notifies it.
So the correct code would be something like this
you cannot call wait, notify and notifyAll in static methods because they are non-static methods in Object class and you need the this reference (which is like an object not a method) to access them...
[Edit: Removed the logical error that Santiago and Srilatha are referring below in this
thread]
[ December 03, 2008: Message edited by: Ankit Garg ]