dmfrl
dmfrl

Reputation: 337

How to implement non-static method in a static class?

Is there a way to implement wait(); (non-static method) in a static context. For example:

public static void getkeylist(List keylist){
   for (int i=0;i<keylist.size();i++){
         System.out.println(keylist.get(i));
         wait(1000);   
   }
}

I am getting an error "Non-static method wait(long) cannot be referred from static context."

Please help me to solve my problem. Thank you in advance.

Upvotes: 2

Views: 4135

Answers (3)

Eve Freeman
Eve Freeman

Reputation: 33145

How about Thread.sleep(1000);

wait is really used for thread control, along with notify. I think you're confused between the method names.

Upvotes: 4

Boni
Boni

Reputation: 610

Create non static method class object and used it.

.....
Foo foo = new Foo();
foo.wait();
.....

Upvotes: 0

Gelin Luo
Gelin Luo

Reputation: 14373

suppose your class is call Foo, you can call Foo.class.wait(1000), or alternatively you can define a static object and call wait method on it:

private static final Object lock = new Object();
...

lock.wait();

Upvotes: 1

Related Questions