Java主方法的方法签名是:
public static void main(String[] args) {
...
}
为什么这个方法必须是静态的?
Java主方法的方法签名是:
public static void main(String[] args) {
...
}
为什么这个方法必须是静态的?
当前回答
为什么公共静态无效主(字符串[]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);
... ...
其他回答
这只是一种惯例。JVM当然可以处理非静态的主方法,如果这是惯例的话。毕竟,您可以在类上定义静态初始化器,并在到达main()方法之前实例化无数对象。
原型public static void main(String[])是在JLS中定义的约定:
方法main必须声明为public、static和void。它必须指定一个形式形参(§8.4.1),其声明类型为String数组。
在JVM规范5.2中。虚拟机启动我们可以读到:
The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.
有趣的是,在JVM规范中并没有提到主方法必须是静态的。 但是规范还说Java虚拟机执行以下两个步骤:
链接初始类(5.4。链接) 初始化它。初始化)
类或接口的初始化包括执行类或接口的初始化方法。
在2.9。特殊方法:
定义一个类或接口初始化方法:
一个类或接口最多有一个类或接口初始化方法,并通过调用该方法进行初始化(§5.5)。类或接口的初始化方法具有特殊名称<clinit>,不带参数,且为空。
类或接口初始化方法不同于定义如下的实例初始化方法:
在Java虚拟机级别,每个用Java编程语言(JLS§8.8)编写的构造函数都作为实例初始化方法出现,具有特殊名称<init>。
因此JVM初始化一个类或接口初始化方法,而不是实例初始化方法,后者实际上是一个构造函数。 因此,他们不需要在JVM规范中提到主方法必须是静态的,因为在调用主方法之前不创建实例这一事实暗示了这一点。
在main方法被调用之前,没有对象被实例化。使用static关键字意味着可以在不创建任何对象的情况下调用方法。
基本上,我们将这些数据成员和成员函数设置为STATIC,它们不执行任何与对象相关的任务。对于main方法,我们将其设置为STATIC,因为它与object无关,因为无论我们是否创建对象,main方法总是运行。
静态方法不需要任何对象。它直接运行,所以main直接运行。