为什么main应该出现在Java类中?

Why should main be present in a Java class?

本问题已经有最佳答案,请猛点这里访问。

为什么要声明一个main方法?我知道main方法是类的起点。但是为什么我们只应该把它声明为public static void main(String args[])

为什么我们不能宣布它为public static void test(String args[])main是关键字吗?JVM如何知道它将是起点?


(P)The JVM has to know some where the starting point of your program is.The way the designers chose to mark that is to call the entry method EDOCX1 original 0,because the same name is used for that purpose in C(which most programmers were familiar with the time)and it makes sense.他们对自己的名字没有特殊的魔法,他们只是有选择,而且这是一个实际的选择。(p)


(P)When a program begins,it has to begin from some where…好吧,这是EDOCX1的音标0(p)(P)为什么停?(p)

  • (P)当JVM呼吁主要的方法,有非目标的存在对于阶级的呼唤。So it has to have static method to allow this from class.(p)

(P)Why Public?(p)

  • The main method is called by the JVM to run the method which is outside the scope of project.

(P)Why Void?(p)

  • Think about it,once the main method finishes,it doesn't mean that the program finished.如果空间是一个新的威胁,它可能是这些威胁仍然在运行。


why should we declare main method? I know that main method is the starting point of the class. But why should only we declare as public static void main(String args[])?

基本上,因为这就是它的定义。(他们决定使用"主"而不是其他名称的原因是,"主"是用于C或C++程序的入口点的名称。)

Why can't we declare as public static void test(String args[])?

因为这样,JVM就无法找到入口点。假设入口点方法可以是任何东西。现在考虑这个例子:

1
2
3
4
5
public class Test {
    public static void foo(String[] args) { ... }

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

…这个命令行…

1
$ java Test

调用哪个方法?foo方法?bar方法?

Is that main key word?

不,它只是一个众所周知的方法名。就核心Java语言而言,EDCOX1×2的方法只是一种具有特定签名的方法。实际上,您可以在同一类中使用具有不同签名的其他main方法。

How will JVM knows that it will be the starting point?

因为规定了JVM使用main方法作为起点。

Why should main be present in a Java class?

好。。。你不必在每个类中都有一个main方法…或者说任何一类…如果您有其他方法来启动对入口点使用不同约定的JVM。但是,如果希望应用程序可以运行,则至少需要一个具有合适的main方法的类。


(P)There can be a number of methods in your class which can be public and static.How can JVM decide which method is the starting program.To avoid ambiguity,the Java language decided a particular standard for the method to be executed by JVM.This standard says that"a self executable class should have a method with following signature:(p)字母名称(P)This standard help JVM is how(p)(P)Public:It allow jvm to access and execute method of your classStatic:the method can be executed just by class name(without making object)Void:returns nothing(JVM expects nothing from your method)Main:In order to make your class self executable,the JVM look for this method name and life of your program is valid from the first statement of this method to last statement of this method.As soon as the last statement of this method is executed,your program terminates and instance of JVM dies.This name standard also helps JVM to find a particular entry point.如果JVM得到任何名称,这将是多个入境点和JVM会混淆不清。所以他们决定一个单一的入口点,并呼吁它主要。(p)


(P)You can compile any Java class without a main method,but a standalone application can't run without a main(……)method.(p)(P)The main method is the method that's defined to be called at the start of an application.没有它,没有地方开始运行。(p)


(P)JVM follows a specification and shares that specification in form JLS with developers.JVM in its specificaton mentions that main method in the following form is the starting point for any standalone Java application:(p)字母名称(P)从Specs:(p)(P)Hence as developers we should adhere to the JVM specification so that JVM can understand what we are trying to do.(p)(P)If my answer is not descriptive or understandable they follow the specification:(p)布尔奇1(P)阅读更多:(p)(P)http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html(p)


(P)EDOCX1-compiler need not create an object to enter into the methodEDOCX1(英文)2-Java is platform independent.Hence if it returns some thing,it may vary from platform to platform(p)(P)EDOCX1 3-for access(p)(P)Main(……)is required so that compiler get a door to enter into your application.As the name implies,it is the main method that should be executed first.(p)