Java - public static void main()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2486207/
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
Java - public static void main()
提问by sgokhales
Should there be any specific order in which I should write the following for a Java main method?
我应该为 Java main 方法编写以下内容的特定顺序吗?
public static void main()
public static void main()
In other words, can I re-shuffle public, static, void in any order?
换句话说,我可以以任何顺序重新洗牌 public、static、void 吗?
Why or why not?
为什么或者为什么不?
采纳答案by Laurence Gonsalves
void
is the return type, so it must go last. The others can be shuffled (see section 8.4 of the Java Language Specificationfor more details on this), but by convention the access modifier usually goes before most of the other method modifiers, except for annotations which usually go first (again, just by convention).
void
是返回类型,所以它必须放在最后。其他的可以混洗(有关这方面的更多详细信息,请参阅Java 语言规范的第 8.4 节),但按照惯例,访问修饰符通常在大多数其他方法修饰符之前,除了通常首先出现的注释(同样,只是按照约定) )。
回答by Zaki
In short, NO, you cant The method name should be immediately prefixed with the return type of the method.Thats part of the method signature. Having the access specifier first is convention though.
简而言之,NO,你不能 方法名称应该立即以方法的返回类型作为前缀。那是方法签名的一部分。不过,首先使用访问说明符是约定俗成的。
回答by Michael Aaron Safyan
The signature for main needs to be:
main 的签名需要是:
public static void main(String[] args){ // Insert code here }
However, there is no requirement that one method be placed before another method. They can be in whatever order you like. Additionally, Java uses a two-pass mechanism so that even if you use some other method in your "main" method, that method can actually appear later in the file. There is no requirement for forward declaration as in C and C++ because of this multi-pass approach taken by Java.
但是,不要求将一种方法置于另一种方法之前。它们可以按您喜欢的任何顺序排列。此外,Java 使用两遍机制,因此即使您在“main”方法中使用其他方法,该方法实际上也可以出现在文件的后面。由于 Java 采用了这种多通道方法,因此不需要像在 C 和 C++ 中那样进行前向声明。
The modifiers public and static can be shuffled; however, by convention, the access modifier (public, private, protected) is always given first, static and/or final (if applicable) are given next, followed by the return-type.
修饰符 public 和 static 可以混洗;然而,按照惯例,总是首先给出访问修饰符(public、private、protected),然后给出 static 和/或 final(如果适用),然后是返回类型。
回答by Steve
You could have easily tried out the various permutations to see what does and does not work. For one thing, none of them will work if you don't change main()
to main(String[] args)
. Beyond that, public
and static
are modifiers that cancome in any order, but most code style conventions have a prescribed order for them anyway. The void
mustbe directly before the method name, since it's the return type, and not a modifier.
您可以轻松地尝试各种排列来查看哪些有效,哪些无效。一方面,如果您不更改main()
为main(String[] args)
. 除此之外,public
并且static
是修饰可以进来任何顺序,但大多数代码风格规范有规定的顺序为他们反正。本void
必须是直接的方法名称之前,因为它的返回类型,而不是修改。
回答by JavaTechnical
We can write, we can interchange static
and public
我们可以写,我们可以交换static
和public
static public void main(String args[])
static public void main(String... args)
However you cannot reshuffle the return type with any position, for e.g.
但是,您不能使用任何位置重新调整返回类型,例如
public void static main(String[] args) // is wrong
and also
并且
static void public main(String[] args) // is also wrong