Reputation: 6527
static public final int i = 0;
public static final int i = 0;
Both working fine.
Why the same thing can be done in two different styles?
Upvotes: 1
Views: 80
Reputation: 5995
From section 8.3.1 (Field Modifiers)of the Language Specification:
"If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier." http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#78091
So the answer is: It's a language specification
Upvotes: 1
Reputation: 178253
The Java Language Specification, Section 8.3.1, allows it:
FieldModifiers:
FieldModifier FieldModifiers FieldModifier
FieldModifier: one of
Annotation public protected private static final transient volatile
This restriction:
It is a compile-time error if the same modifier appears more than once in a field declaration, or if a field declaration has more than one of the access modifiers public, protected, and private.
and
If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.
So, public static
and static public
are allowed.
Upvotes: 3
Reputation: 41168
Because there is no reason to block it.
Neither operator has any precedence or effect on the other, you can put all the keywords before the variable (for example volatile as well) in any order. That's just the way the language is defined.
The general style tends be to have access level first, then static if present, then anything else. That's not even an official guideline though (that I know of), just what most people do.
Upvotes: 4