Java主方法的方法签名是:
public static void main(String[] args) {
...
}
为什么这个方法必须是静态的?
Java主方法的方法签名是:
public static void main(String[] args) {
...
}
为什么这个方法必须是静态的?
当前回答
静态方法不需要任何对象。它直接运行,所以main直接运行。
其他回答
让我用更简单的方式来解释这些事情:
public static void main(String args[])
除了applet,所有Java应用程序都从main()开始执行。
关键字public是一个访问修饰符,允许从类外部调用成员。
使用Static是因为它允许调用main()而不必实例化该类的特定实例。
Void表示main()不返回任何值。
The main method of the program has the reserved word static which means it is allowed to be used in the static context. A context relates to the use of computer memory during the running of the program. When the virtual machine loads a program, it creates the static context for it, allocating computer memory to store the program and its data, etc.. A dynamic context is certain kind of allocation of memory which is made later, during the running of the program. The program would not be able to start if the main method was not allowed to run in the static context.
静态方法不需要任何对象。它直接运行,所以main直接运行。
各种类型的applet、midlet、servlet和bean被构造,然后在它们上调用生命周期方法。调用main是对主类所做的全部工作,因此不需要在被多次调用的对象中保存状态。将main固定在另一个类上是很正常的(尽管这不是一个好主意),这将妨碍使用类创建main对象。
如果不是,如果有多个构造函数,应该使用哪个构造函数?
在Java语言规范中有更多关于Java程序初始化和执行的信息。