I am currently learning Java. As soon as I got to the point where Threads were included in the lessons I wondered why Thread.sleep() can be called in the main method.
Idea: The Thread class is static, that is why it doesn`t need to be instantiated. But I wonder why I can create an object of it. How can I understand that?
Short code snippet:
class MyClass{
public static void main(String [] args){
Thread trd = new Thread(new MyThread());
trd.start();
try{
Thread.sleep(1000);
}
catch (InterruptedException e) {
System.out.println("Error occured");
}
}
}
public class SuperClass{
int x = 5;
}
public class MyThread extends SuperClass implements Runnable{
public void run(){
while(x<10){
System.out.println("Hello" + x);
x++;
}
}
}