×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

a program only has one-entry.

public static void main(), by declare main as static, it gives you one and only one starting point of the program.

Also static defines Class variable and method. You can access them via the class name.

For example:

Class A {
public static aClassMethod(){
}
}

You don't need to instantiate an object instance of class A to access aClassMethod.

A.aClassMethod();
will do the job for you Instead of
new A().aClassMethod();

In RMI, the value of static variable will not be passed to the caller, just be careful about it.
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 有一个java问题,请教诸位高手
    public static void main() --Java 的主程序都是这样定义的,public 和 void可以理解,但是为什么要加一个static 呢?
    • you can run it without an instance of the class. :))
    • 如果你是初学,暂时不用管他。等掌握了对象和类的概念后就明白了。
    • static意思是说,你不需要建立object,就可以运行这个程序,它对于整个project来说,是通用的。henhen的说法是对的。
    • a program only has one-entry.
      public static void main(), by declare main as static, it gives you one and only one starting point of the program.

      Also static defines Class variable and method. You can access them via the class name.

      For example:

      Class A {
      public static aClassMethod(){
      }
      }

      You don't need to instantiate an object instance of class A to access aClassMethod.

      A.aClassMethod();
      will do the job for you Instead of
      new A().aClassMethod();

      In RMI, the value of static variable will not be passed to the caller, just be careful about it.
    • 你这样想就明白了:如果main不是静态,谁来实例化它呢?
        • 是呀,我还领悟到了上帝也一定是静态的。
          • 厉害!你咖啡喝多了,思维贼跃
          • 你们楼上两位合起来就是“屁精”:)
    • 任何编程语言,其2进制执行代码必须有一个entry point
      就如C语言,如果你读过crt0.asm, 就知道main是个约定俗成的
      entry point.

      其实,它不一定要叫main,比如winmain, _tmain都可以。
      例如MFC的进入点就是一个实例化了的app类。

      static main 不过就是放在了代码段的一个普通function。

      如果我创造编程语言,entry point 一定是interview()
      • Well, My entry point will be "hoho()"
    • 谢谢各位!看来学编程还是需要一点悟性的
      • 咱们互通有无,你教俺抄股,俺教你JAVA。
        • I need a cat, I have dogs....