9

Should there be any specific order in which I should write the following for a Java main method?

public static void main()

In other words, can I re-shuffle public, static, void in any order?

Why or why not?

3
  • 24
    You're asking a lot of these questions in very rapid succession. I encourage you to read the Java language specification ( java.sun.com/docs/books/jls/third_edition/html/j3TOC.html ) which will answer many of these inquiries. Some experimentation with your compiler will also be useful. Commented Mar 21, 2010 at 6:33
  • 8
    @John Feminella - It is nice to see that someone can recognize that the OP has asked many questions that are introductory in nature yet somehow is able to withstand the infantile urge to make snarky, self-important commentary. Your comment was classy and helpful - bravo! Commented Mar 21, 2010 at 6:37
  • 1
    @ John : Thanks for suggestion.Will do it. Commented Mar 25, 2010 at 15:00

5 Answers 5

21

void is the return type, so it must go last. 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).

1
  • JLS 8.4.3: "If two or more (distinct) method modifiers appear in a method declaration, it is customary, though not required, that they appear in the order consistent with that shown:" Annotation public protected private abstract static final synchronized native strictfp
    – Andreas
    Commented May 12, 2016 at 5:35
6

We can write, we can interchange static and 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
1
  • Quick java question, can you do static public void main() without String args[] or String... args or is that generally discouraged?
    – M Y
    Commented Jun 22, 2015 at 7:07
1

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. There is no requirement for forward declaration as in C and C++ because of this multi-pass approach taken by Java.

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.

2
  • You are confused. He is asking about the order of "public" "static" "void" and "main" Commented Mar 21, 2010 at 6:34
  • @Kevin, yes, I see. I've now answered both. Commented Mar 21, 2010 at 6:34
1

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 can come in any order, but most code style conventions have a prescribed order for them anyway. The void must be directly before the method name, since it's the return type, and not a modifier.

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.