Why we have to use Thread.sleep()
instead of just sleep()
in Java? What are reason, that forces developers to use "longer" version?

- 17,148
- 27
- 124
- 216

- 256
- 2
- 14
-
2Explicitly letting readers of the code know where the method is coming from. You can use static import to ise just sleep() – amit Oct 23 '14 at 07:08
-
1Because `sleep` is a static method of `Thread`... – MadProgrammer Oct 23 '14 at 07:13
3 Answers
Because sleep()
is a static method of Thread
. You could import it static, and then use sleep()
import static java.lang.Thread.sleep;
public static void main( String[] args )
{
try
{
sleep( 1000 );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
}
see here: http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html
see also here (https://stackoverflow.com/a/421127/461499) on why to use static imports sparelingly.

- 1
- 1

- 19,195
- 10
- 76
- 121
-
If the Thread class is extended .. even we can use this.sleep() right ? – Proceso Oct 23 '14 at 07:23
-
1You can, but shouldn't. What if you override sleep()? Use Thread.sleep() or MyExtendedThread.sleep(). – Rob Audenaerde Oct 23 '14 at 07:27
-
Yeagh and if it's extended and overriden then static import of sleep method won't work right?. so after all using the Thread.sleep() seems better. – Proceso Oct 23 '14 at 07:40
-
I tried and checked. You cannot really override `sleep()` as it is a native method. You can create a static method `sleep()` in a subclass. But it will never be called somehow.. Weird. – Rob Audenaerde Oct 23 '14 at 07:52
-
See this answer for a good explanation: http://stackoverflow.com/a/24448585/461499 – Rob Audenaerde Oct 23 '14 at 07:54
-
I just wanted to say, It's always better to call the Thread.sleep() method than importing the static method and calling just sleep() method. – Proceso Oct 23 '14 at 07:58
-
@RobAu You can override native methods. You cannot override static methods but you can shadow them: `MyThread.sleep(1000)` – xehpuk Oct 23 '14 at 07:59
-
@xehpuk, I tried that; `public static void sleep()` in `MyThread` but the result is that `Thread.sleep()` is still called when `MyThread.sleep()` in the code. – Rob Audenaerde Oct 23 '14 at 08:04
-
You are correct, I tried it again and saw I had a mistake in the method signature... Then it will never be called :o – Rob Audenaerde Oct 24 '14 at 06:50
Java is an object oriented language - every method ("function") must belong to a class, even if it's a static
method (consider, e.g., that there's no "global" print
function - you must call System.out.print
). If it really annoys you, though, you could statically import the method:
import static java.lang.Thread.sleep
public class MyClass {
public static void main (String[] args) {
sleep (10);
}
}

- 297,002
- 52
- 306
- 350
sleep()
method in Thread
class is a static
method. We don't access static
methods using the instance object.
Both the this.sleep()
and Thread.sleep()
are the same but it's against the conventions.
static
keyword means it's common to all the classes there for if you use an instance variable to access a static
method it's not clear.