/**
* Returns a reference to the currently executing thread object.
*
* @return the currently executing thread.
*/publicstatic native Thread currentThread();
Static member \'java.lang.Thread.sleep(long)\' accessed via instance reference less... (Ctrl+F1) Shows references to static methods and fields via class instance rather than a class itself.
In Java, sleep is a static method. Both your examples do exactly the same thing, but the former version is confusing because it looks like it is calling a method on a specific object but it\'s not doing that at all. In your example it won\'t matter much, but it is more dangerous if you have the following:
someOtherThread.sleep(x);
This time it looks like you are telling some other thread to sleep, but in fact you are putting the current thread to sleep. The way to avoid making this type of mistake is to always call static methods using the class rather than a specific object.
The way to avoid making this type of mistake is to always call static methods using the class rather than a specific object. —— 避免了“调用一个实例的静态方法,而实际上应该是调用一个类的静态方法” 之类的错误。 其实说白了也就是避免这样的错误: someOtherThread.sleep(x);