Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem on order of public and static of main method  RSS feed

 
sahil kumar garg sangrur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

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.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there is a preferred order: public should precede static.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice find Claudiu. It is actually very fine print.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done finding that fine print
 
sahil kumar garg sangrur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!