Java主方法的方法签名是:
public static void main(String[] args) {
...
}
为什么这个方法必须是静态的?
Java主方法的方法签名是:
public static void main(String[] args) {
...
}
为什么这个方法必须是静态的?
当前回答
因为静态成员不是任何特定类的一部分,而且主方法不需要创建它的Object,但仍然可以引用所有其他类。
其他回答
为什么公共静态无效主(字符串[]args) ?
这就是Java语言和Java虚拟机是如何设计和编写的。
Oracle Java语言规范
请参阅第12章执行- 12.1.4节调用Test.main:
最后,在完成Test类的初始化之后(在此期间可能发生了其他相应的加载、链接和初始化),调用Test的main方法。 方法main必须声明为public、static和void。它必须接受一个字符串数组的参数。此方法可以声明为任意一种 public static void main(String[] args) 或 public static void mainargs)
Oracle Java虚拟机规范
请参阅第2章Java编程语言概念-第2.17节执行:
Java虚拟机通过调用某个指定类的main方法并向其传递一个参数(字符串数组)来开始执行。这将导致指定的类被加载(§2.17.2),链接(§2.17.3)到它使用的其他类型,并初始化(§2.17.4)。方法main必须声明为public、static和void。
Oracle OpenJDK源代码
下载并提取jar源代码,看看JVM是如何编写的,查看../launcher/java. C,其中包含java [-options] class [args…]命令背后的原生C代码:
/*
* Get the application's main class.
* ... ...
*/
if (jarfile != 0) {
mainClassName = GetMainClassName(env, jarfile);
... ...
mainClass = LoadClass(env, classname);
if(mainClass == NULL) { /* exception occured */
... ...
/* Get the application's main method */
mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
"([Ljava/lang/String;)V");
... ...
{ /* Make sure the main method is public */
jint mods;
jmethodID mid;
jobject obj = (*env)->ToReflectedMethod(env, mainClass,
mainID, JNI_TRUE);
... ...
/* Build argument array */
mainArgs = NewPlatformStringArray(env, argv, argc);
if (mainArgs == NULL) {
ReportExceptionDescription(env);
goto leave;
}
/* Invoke main method. */
(*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
... ...
public static void main(String args[])是什么意思?
public is an access specifier meaning anyone can access/invoke it such as JVM(Java Virtual Machine. static allows main() to be called before an object of the class has been created. This is neccesary because main() is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class. class demo { private int length; private static int breadth; void output(){ length=5; System.out.println(length); } static void staticOutput(){ breadth=10; System.out.println(breadth); } public static void main(String args[]){ demo d1=new demo(); d1.output(); // Note here output() function is not static so here // we need to create object staticOutput(); // Note here staticOutput() function is static so here // we needn't to create object Similar is the case with main /* Although: demo.staticOutput(); Works fine d1.staticOutput(); Works fine */ } } Similarly, we use static sometime for user defined methods so that we need not to make objects. void indicates that the main() method being declared does not return a value. String[] args specifies the only parameter in the main() method. args - a parameter which contains an array of objects of class type String.
任何应用程序的真正入口点都是静态方法。如果Java语言支持实例方法作为“入口点”,那么运行时将需要在内部将其作为静态方法实现,该方法构造对象的实例,然后调用实例方法。
有了这些,我将研究选择以下三个选项中的一个的基本原理:
我们今天看到的静态void main()。 在新构造的对象上调用的实例方法void main()。 使用类型的构造函数作为入口点(例如,如果入口类被称为Program,那么执行将有效地由新的Program()组成)。
分解:
静态无效主()
调用外围类的静态构造函数。 调用静态方法main()。
void main ()
调用外围类的静态构造函数。 通过有效调用new ClassName()构造外围类的实例。 调用实例方法main()。
新ClassName ()
调用外围类的静态构造函数。 构造类的实例(然后不做任何操作,只是返回)。
理由是:
这个我倒着讲。
请记住,Java的设计目标之一是强调(尽可能要求)良好的面向对象编程实践。在这种情况下,对象的构造函数初始化对象,但不应该对对象的行为负责。因此,给出new ClassName()入口点的规范会使新Java开发人员感到困惑,因为它强制每个应用程序上的“理想”构造函数设计出现异常。
通过将main()设置为实例方法,可以解决上述问题。但是,它要求规范列出入口类的构造函数的签名以及main()方法的签名,从而增加了复杂性。
总而言之,指定静态void main()将创建复杂度最低的规范,同时遵循将行为置于方法中的原则。考虑到实现main()方法本身构造类的实例并调用实例方法是多么简单,将main()指定为实例方法并没有真正的优势。
static -当JVM调用主方法时,被调用的类不存在对象,因此它必须有静态方法来允许从类调用。
我不知道JVM是否在对象实例化之前调用main方法…但是main()方法是静态的还有一个更有力的原因…当JVM调用类的主方法(比如Person)时。它通过"Person.main()"调用它。您可以看到,JVM通过类名调用它。这就是为什么main()方法应该是静态和公共的,以便JVM可以访问它。
希望有帮助。如果是的话,请在评论中告诉我。