Java 执行程序时调用的第一个方法是哪个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4194407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is the first method called when Java executes a program?
提问by Nimit Parekh
I am learning core Java and I have one question, "Which is the first method called when the program is executed?"
我正在学习核心 Java,我有一个问题,“执行程序时调用的第一个方法是哪个?”
回答by aioobe
That would be the main
method.
那就是main
方法。
It should be declared as
它应该声明为
public static void main(String[] args)
- It needs to be
public
, since the JVM should have access to call the method. - It needs to be
static
, as no objects are instantiated when the program starts - It takes an array of
String
s as argument (coming from the command-line)
- 它必须是
public
,因为 JVM 应该有权调用该方法。 - 必须如此
static
,因为程序启动时没有对象被实例化 - 它需要一个
String
s数组作为参数(来自命令行)
Some good links to have a look at:
一些不错的链接可以看看:
- The main Method (from the official Getting started trail)
- What is the main method
- Entry point for Java applications: main(), init(), or run()?
Some people may recommend you to write
有些人可能会推荐你写
public static void main(String... args)
this is equivalent to String[] args
ifyou're using a compiler of version 1.5 or later. (I would discourage this unless you're extensively calling your main
method internally with a varying number of arguments.)
这相当于String[] args
,如果你使用的1.5或更高版本的编译器。(除非您在main
内部使用不同数量的参数广泛调用您的方法,否则我不鼓励这样做。)
Others may suggest
其他人可能会建议
public static void main(String args[])
This is also equivalent, but discouraged by the Java Coding Convention.
这也是等效的,但Java Coding Convention不鼓励这样做。
回答by Tom Anderson
It's usually main. But in this program, it's pain:
通常是主要的。但是在这个程序中,这是痛苦的:
public class WhatThe {
public static final int x = pain();
public static int pain() {
System.out.println("pain!");
return 0;
}
public static void main(String[] args) {
System.out.println("main");
}
}
As it is in this one:
正如它在这个:
public class WhatThe {
static {
pain();
}
public static void pain() {
System.out.println("pain!");
}
public static void main(String[] args) {
System.out.println("main");
}
}
This is unlikely to be useful knowledge, but it's something to be aware of.
这不太可能是有用的知识,但需要注意。
回答by Jigar Joshi
public static void main(String ar[])
Java programs start executing at the main method, which has the following method heading:
Java 程序从 main 方法开始执行,该方法具有以下方法标题:
public static void main(String[] args)
public static void main(String... args) //java 1.5+
public static void main(String args[])
回答by Andreas Dolk
In addition to aioobes answer
除了 aioobes 回答
A usual way to start a simple java program is to execute java like this:
启动一个简单的java程序的通常方法是像这样执行java:
java com.example.MyClass
com.example.MyClass
(or your fully qualified class name) needsto have a main method with exactly this signature:
com.example.MyClass
(或您的完全限定类名)需要有一个具有此签名的 main 方法:
public static void main(String[] args)
(you're only allowed to change the name of the parameter, like arguments
instead of args
). The virtual machine will try to load the named class and try to invoke this static method which will "start the Java program".
(您只能更改参数的名称,例如arguments
代替args
)。虚拟机将尝试加载命名类并尝试调用此静态方法,该方法将“启动 Java 程序”。
回答by Preethi
The main method will be called first,control goes to main method first which has the method headings: public static void main(String[] args) or public static void main(String args[])
将首先调用 main 方法,控制首先转到具有方法标题的 main 方法: public static void main(String[] args) 或 public static void main(String args[])