main()方法

来源:互联网 发布:mac如何固定dock 编辑:程序博客网 时间:2024/06/17 10:34

一个类中main方法在运行该类时,main方法会被自动执行,而当你创建该类的实例时,main不会被自动执行。

main和普通唯一的区别在于,在类被加载时,它会被自动执行。

A.main也是可以的。

 class A{

    public static void main(String args[]){

        System.out.println("hello");

  }

}

 

class B{

   public static void main(String args[]){

       A a_1 = new A();

       System.out.println("hello"+1);

  }

}

 

运行B时,打印信息:

hello+1;

A中的hello并没有被打印出来。