`

public static void main(String args[])

阅读更多

In core Java program, execution starts from main method when you type java main-class-name, JVM search for public static void main(String args[]) method in that class and if it doesn't find that method it throws error NoSuchMethodError:main and terminates.

First, why public? 可从任何地方访问此类成员

Propose it is private, then this method can not access outside the class, how jvm excute it?

Because main() method can excute in any java runtime evnironment, JVM need to access and execute it.

Any method or variable which is declared public in Java can be accessible from outside of that class, so declare public.

 

Second, why static? main()方法不应依赖于要创建的任何类的实例

Propose removed static, in order to excute main(), JVM has to create instance of main Class.

but constructor can be overloaded and can have arguments, ther would be no consistent way for JVM to create instance and find main method 

Thrid, why void?  不返回任何值

Main() can be overloaded, from the entry point of  class, it is not supposed to return any value.

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics