Professional Documents
Culture Documents
problem on order of public and static of main method (Beginning Java forum at JavaRanch)
File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Java FAQ
Recent Topics
Register / Login
Author
sahil kumar garg sangrur Greenhorn Joined: Jun 05, 2012 Posts: 10
Hello Friends please help to get me out from this problem static public void main(String arg[]){} is this same with public static void main(String args[]){} why there is not any particular order of static and public in java why this statement doesn't give compile time error
Mansukhdeep Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1156
I like...
Read this from the JLS. All that one needs to be careful about is what they have mentioned there. Specifically,
It is a compile-time error if the same modifier appears more than once in a method declaration, or if a method declaration has more than one of the access modifiers public, protected, and private (6.6).
www.coderanch.com/t/611479/java/java/order-public-static-main-method
1/4
7/14/13
problem on order of public and static of main method (Beginning Java forum at JavaRanch)
It is a compile-time error if a method declaration that contains the keyword abstract also contains any one of the keywords private, static, final, native, strictfp, or synchronized. It is a compile-time error if a method declaration that contains the keyword native also contains strictfp.
The order is irrelevant for method modifiers as they are optional in the first place. You need to be cautious about the combinations mentioned above though.
~ Mansukh Campbell Ritchie Sheriff Joined: Oct 13, 2005 Posts: 32917
For readability and consistency of code it is preferred that you write the access modifier before static. As you have already seen, it will compile without. I found these suggestions on Mindprod about order of modifiers, and people say there is a section in the JLS, but I couldn't find it.
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 above in the production for MethodModifier.
Claudiu
Mansukhdeep
www.coderanch.com/t/611479/java/java/order-public-static-main-method 2/4
7/14/13
problem on order of public and static of main method (Beginning Java forum at JavaRanch)
sahil kumar garg sangrur Greenhorn Joined: Jun 05, 2012 Posts: 10
Hello Friends please help to get me out from this problem static public void main(String arg[]){} is this same with public static void main(String args[]){} why there is not any particular order of static and public in java why this statement doesn't give compile time error
why the static and public are interchangeable in java Thanks in advance!!!
Mike Simmons Ranch Hand Joined: Mar 05, 2008 Posts: 2837
You mean, why are their positions interchangeable? That's just the way they wrote the rules. If they had required a particular order, you could just as easily ask, why that order, rather than some other order? It doesn't really matter, and in this case, they chose to make no rule, and let people do what they wanted. Over time, most people have standardized on doing it the way recommended in the JLS. That's just the way things are.
7/14/13
problem on order of public and static of main method (Beginning Java forum at JavaRanch)
Similar Threads how to complie/run a java prog with 2 or more classes Java class 1 Problem in javac and java command [this]....
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:26:13 .
www.coderanch.com/t/611479/java/java/order-public-static-main-method
4/4