• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the error??  RSS feed

 
Salma Benmkhtar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
this is the function:

public static double getTime ( ){
int seconds=0;;
while(true)
{
wait(1000);
currenttime=seconds++;
}

return currenttime;
}
the error is :non-static method getTime() cannot be referenced from staitc context
it's may be because wait() is not static !!
is there any other possible solution (i.e if u know another function that works exactly like wait() but is static??)without removing the static from my function getTime().
thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() does something special, unrelated to what you want here.
You want the static method Thread.sleep(); use that instead.
I don't think this method will compile anyway, though, as the last line is unreachable. Not sure what you want this method to do, really.
This ought to be moved to JiG(I).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Intermediate...
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this method will compile anyway, though, as the last line is unreachable.

It also won't compile because of the static context of the method and the checked exception that wait(long) can throw as well (need a try/catch block). Even if it did compile it would throw IllegalMonitorStateException because the block is not sychronized. Thread.sleep(long) is definitely what you want and as Ernest also pointed out, you need a means to bug out of the while loop.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!