Java主方法的方法签名是:

public static void main(String[] args) {
    ...
}

为什么这个方法必须是静态的?


当前回答

原型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规范中提到主方法必须是静态的,因为在调用主方法之前不创建实例这一事实暗示了这一点。

其他回答

这只是一种惯例,但可能比另一种更方便。使用静态主程序,调用Java程序所需要知道的只是类的名称和位置。如果它不是静态的,您还必须知道如何实例化该类,或者要求该类具有空构造函数。

这只是一种惯例。JVM当然可以处理非静态的主方法,如果这是惯例的话。毕竟,您可以在类上定义静态初始化器,并在到达main()方法之前实例化无数对象。

使用Java命令运行Java虚拟机(JVM)时,

java ClassName argument1 argument2 ...

当您执行您的应用程序时,您指定它的类名作为java命令的参数,如上所述

JVM尝试调用您指定的类的主方法

-在这一点上,类的对象还没有被创建。 将main声明为静态允许JVM在不创建的情况下调用main 类的实例。

让我们回到命令

ClassName是JVM的命令行参数,它告诉JVM要执行哪个类。在ClassName之后,您还可以指定一个字符串列表(由空格分隔)作为JVM将传递给应用程序的命令行参数。-这些参数可以用来指定运行应用程序的选项(例如文件名)-这就是为什么在main中有一个名为String[] args的参数

参考资料:Java™如何编程(早期对象),第十版

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.

Static表示该方法是类方法。并且调用时不需要任何类的对象。