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

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

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

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

应用程序必须从某个地方开始,是吗?从《时代的黎明》(1972?)EDCOX1的0个字已经走了,为什么要用Java来改变它呢?

@当然,这个问题的措辞不清楚,我会修改的。问题似乎在于为什么该方法被命名为main,而不是为什么它需要是静态的。

这不是关键字。"JVM如何知道这是起点?">通过读取其名称"main"

@chrylis the op询问为什么名字main,这似乎在我上面提到的问题中得到了回答。

@桑巴,对不起,第二个答案漏了。

(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.如果空间是一个新的威胁,它可能是这些威胁仍然在运行。

据我所知,问题是关于main的名字,而不是签名的其余部分。

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就无法找到入口点。假设入口点方法可以是任何东西。现在考虑这个例子:

public class Test {

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

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

}

…这个命令行…

$ 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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值