简体   繁体   中英

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.

How about Thread.sleep(1000);

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

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();

Create non static method class object and used it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM