简体   繁体   English

Java - public static void main()

[英]Java - public static void main()

Should there be any specific order in which I should write the following for a Java main method? 是否应该有任何特定的顺序,我应该为Java main方法编写以下内容?

public static void main()

In other words, can I re-shuffle public, static, void in any order? 换句话说,我可以按任何顺序重新洗牌,静态,无效吗?

Why or why not? 为什么或者为什么不?

void is the return type, so it must go last. void是返回类型,因此它必须最后。 The others can be shuffled (see section 8.4 of the Java Language Specification for 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). 其他的可以改组(参见Java语言规范的第8.4节了解更多细节),但按照惯例,访问修饰符通常在大多数其他方法修饰符之前,除了通常首先注释的注释(再次,按照惯例) )。

We can write, we can interchange static and public 我们可以写,我们可以互换staticpublic

static public void main(String args[])

static public void main(String... args)

However you cannot reshuffle the return type with any position, for eg 但是,您无法对任何位置的返回类型进行重新洗牌,例如

public void static main(String[] args) // is wrong

and also 并且

static void public main(String[] args) // is also wrong

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. 简而言之,NO,你不能方法名称应该立即以方法的返回类型为前缀。方法签名的一部分。 Having the access specifier first is convention though. 首先使用访问说明符是惯例。

The signature for main needs to be: 主要签名需要:

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. 此外,Java使用双向机制,因此即使您在“main”方法中使用其他方法,该方法实际上也可以在文件的后面出现。 There is no requirement for forward declaration as in C and C++ because of this multi-pass approach taken by Java. 由于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,private,protected),然后给出静态和/或最终(如果适用),然后是return-type。

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) . 首先,如果你不将main()更改为main(String[] args) ,它们都不会起作用。 Beyond that, public and static are modifiers that can come in any order, but most code style conventions have a prescribed order for them anyway. 除此之外, publicstatic可以按任何顺序排列的修饰符,但大多数代码样式​​约定无论如何都有规定的顺序。 The void must be directly before the method name, since it's the return type, and not a modifier. void 必须直接在方法名称之前,因为它是返回类型,而不是修饰符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM