Java: why Thread.sleep() and yield() are static?

后端 未结 6 397
庸人自扰
庸人自扰 2021-01-03 19:56

Why sleep() and yield() methods are defined as static methods in java.lang.Thread class?

6条回答
  •  盖世英雄少女心
    2021-01-03 20:56

    Both sleep and yield methods are native. To understand better from above answers I made two classes ClassA and ClassB with same static method. I invoked method of other class to check its behavior. So we can call other class' static method.
    So may be there is other reason behind making sleep method static.

    public class ClassA {
    
        public static void method(){
            System.out.println("Inside ClassA method");
        }
    
        public static void main(String[] args) {
            method();
            ClassB classb = new ClassB();
            classb.method();
        }
    }
    
        public class ClassB {
        public static void method(){
            System.out.println("Inside ClassB method");
        } 
    
    }
    

提交回复
热议问题