Java主方法的方法签名是:
public static void main(String[] args) {
...
}
为什么这个方法必须是静态的?
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规范中提到主方法必须是静态的,因为在调用主方法之前不创建实例这一事实暗示了这一点。
其他回答
最近,类似的问题也出现在了程序员网站上。SE
为什么在Java和c#中使用静态主方法,而不是构造函数? 从主要或次要来源中寻找一个明确的答案,为什么(特别是)Java和c#决定将静态方法作为它们的入口点-而不是通过应用程序类的实例表示应用程序实例,入口点是一个适当的构造函数?
公认的答案是,
In Java, the reason of public static void main(String[] args) is that Gosling wanted the code written by someone experienced in C (not in Java) to be executed by someone used to running PostScript on NeWS For C#, the reasoning is transitively similar so to speak. Language designers kept the program entry point syntax familiar for programmers coming from Java. As C# architect Anders Hejlsberg puts it, ...our approach with C# has simply been to offer an alternative... to Java programmers... ...
这只是一种惯例,但可能比另一种更方便。使用静态主程序,调用Java程序所需要知道的只是类的名称和位置。如果它不是静态的,您还必须知道如何实例化该类,或者要求该类具有空构造函数。
The static key word in the main method is used because there isn't any instantiation that take place in the main method. But object is constructed rather than invocation as a result we use the static key word in the main method. In jvm context memory is created when class loads into it.And all static members are present in that memory. if we make the main static now it will be in memory and can be accessible to jvm (class.main(..)) so we can call the main method with out need of even need for heap been created.
c++、c#和Java中的主要方法是静态的。
这是因为它们可以被运行时引擎调用,而无需实例化任何对象,然后main主体中的代码将完成其余的工作。
该方法是静态的,否则就会产生歧义:应该调用哪个构造函数?特别是如果你的类是这样的:
public class JavaClass{
protected JavaClass(int x){}
public void main(String[] args){
}
}
JVM是否应该调用新的JavaClass(int)?x应该通过什么?
如果不是,JVM是否应该实例化JavaClass而不运行任何构造函数方法?我认为不应该这样,因为那样会使你的整个类出现特殊情况——有时你有一个没有初始化的实例,你必须在每个可能被调用的方法中检查它。
在调用入口点之前,JVM必须实例化一个类,这有太多的边缘情况和模糊性,因此没有意义。这就是为什么main是静态的。
我不知道为什么main总是被标记为public。