是否一定要写对象,类才会被加载???
不是的

 

加static成为静态后,可以直接用类名进行调用,无需创建实例

 

package javastudy;

class StaticCode1{        
    static{            
        System.out.println("x");
    }
    public static void show(){                //静态方法
        System.out.println("gogogo");
    }
}

public class StaticDemo3 {
    public static void main(String[] args) {
        StaticCode1.show();                    //用类名调用静态方法,即可加载。因为static静态方法可以直接用类名调用
    }

}

输出:

x
gogogo

相关文章:

  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2021-10-07
  • 2021-11-18
  • 2022-02-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案