File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes quest no static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "quest no static " Watch "quest no static " New topic

quest no static

Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45

please find below the code.

This code will compile fine but give run time exception. I agree with that but i still believe that there should be a compile time error with this code at line 1, since bm.go() calls method public void go() which is not static.

Also go() calls start() method which is not static . Hence we can't make the line no 2 public static void go() which will give compilation error.

Please let me know the exact problem here

Thanks in advance
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why do you think that there should be a problem with calling non-static methods?

What are the conditions to be satisfied when calling notifyAll (or wait, or notify)? Look at the exception you are getting, you know what it means?
[ March 08, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Andr� Junior

Joined: Jan 08, 2007
Posts: 4
when you call 'wait','notifyAll' and 'notify', you must have the lock
on the object, otherwise you'll get a 'IllegalMonitorStateException' =]
[ March 08, 2007: Message edited by: Andr� Junior ]
Rachil Chandran
Ranch Hand

Joined: Mar 05, 2006
Posts: 67
Think about it. You are not calling go() from a static context, you are calling go() in bm instance's context

SCJP2 96%
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
Posts: 49
From the book: wait(), notify(), and notifyAll() must be called from within a synchronized context! A thread can't invoke a wait or notify method on an object unless it owns that object's lock. See SCJP book, page 720.

Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Thanks to All,

I understood the problem that while calling the go method (bm.go()) it was called from the bm instance's context and not from the static context and thats why even the go method is non static it's not giving any error.

Thanks again
I agree. Here's the link:
subject: quest no static
It's not a secret anymore!