You are on page 1of 4

7/14/13

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

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Beginning Java

Author

problem on order of public and static of main method


posted 5/13/2013 5:31:07 PM

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...

posted 5/13/2013 5:55:33 PM

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

posted 5/13/2013 6:00:22 PM

But there is a preferred order: public should precede static.

Campbell Ritchie Sheriff Joined: Oct 13, 2005 Posts: 32917

posted 5/13/2013 6:14:57 PM

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.

Claudiu Chelemen Ranch Hand Joined: Mar 25, 2011 Posts: 67


I like...

posted 5/13/2013 7:19:40 PM

It's the fine print right at the end of 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 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)

Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1156


I like...

posted 5/13/2013 7:34:26 PM

Nice find Claudiu. It is actually very fine print.

Campbell Ritchie Sheriff Joined: Oct 13, 2005 Posts: 32917

posted 5/13/2013 7:34:30 PM

Well done finding that fine print

sahil kumar garg sangrur Greenhorn Joined: Jun 05, 2012 Posts: 10

posted 5/14/2013 8:43:02 PM


sahil kumar garg sangrur wrote:

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

posted 5/14/2013 10:20:35 PM


sahil kumar garg sangrur wrote:

why the static and public are interchangeable in java

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.

I agree. Here's the link: http://aspose.com/file-tools


www.coderanch.com/t/611479/java/java/order-public-static-main-method 3/4

7/14/13

problem on order of public and static of main method (Beginning Java forum at JavaRanch)

subject: problem on order of public and static of main method

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/611479/java/java/order-public-static-main-method

4/4

You might also like