Modifier Keyword order in Java
Solution 1:
In theory it does not matter if you say public static final or final static public, but if you follow the usual convention, other people will able to read your code more easily. Here is the preferred order:
[ public | protected | private ]
static
abstract
synchronized
[ transient | volatile ]
final
native
strictfp
[ int | long | String | class | enum | interface etc. ]
Solution 2:
Checkstyle (which implements the suggestions of the Java Language Specifications sections, 8.1.1, 8.3.1, and 8.4.3) says:
- public
- protected
- private
- abstract
- default
- static
- final
- transient
- volatile
- synchronized
- native
- strictfp
Solution 3:
The custom usage order of the modifiers is mentioned in the Java Language Specification (so no need to have an own opinion ;-)) e.g. for method modifiers you will find the following definition (extract):
MethodModifiers: MethodModifier MethodModifiers MethodModifier MethodModifier: one of Annotation public protected private abstract static final synchronized native strictfp
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.
You will find this sentence at several other places where the usage of modifiers is specified, e.g. here for field modifiers.
(This is mostly copied from another answer of mine here).
Solution 4:
The "best" would be to follow the Java Coding Style Guide, that states in 6.2 (method declaration):
public static final synchronized long methodName()
throws ArithmeticException, InterruptedException {
static int count;
}
Solution 5:
The best order is the one that the rest of your code uses.