This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

quest no static  RSS feed

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

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
Anvi
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it. You are not calling go() from a static context, you are calling go() in bm instance's context
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Anvi
 
I will suppress my every urge. But not this shameless plug:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!