128
votes

What is a reasonable order of Java modifiers?

  • abstract
  • final
  • native
  • private
  • protected
  • public
  • static
  • strictfp
  • synchronized
  • transient
  • volatile

Update

I have changed the wording from recommended to reasonable in order to calm down the discussions whether the order is recommended or not.

4
It doesn't matter in the slightest, and you shouldn't waste too much time agonizing about it. Personally I always put the access modifier first but after that I couldn't even tell you what I do next. If you want a reference try here, but I'm not confident that it's even specified there. - user207421
I wonder why this should be a "not constructive" question (close requests). You can find the recommendation in the specification (see my answer) and following this recommendation will improve readability of the code. Static code analyzer (like SONAR) will complain if you use a different order. - FrVaBe
I took another look at the Oracle/Sun Java Code Conventions. The matter isn't even mentioned, in the one place where you should look and the one place where you would expect it to appear. - user207421
@EJB The code conventions are from 1999 and strongly need to be updated. I hope that they include this topic if they ever touch these conventions again as it is indeed a good place! - FrVaBe
Can Eclipse sort the modifiers automatically? Or at least give a warning if they are unsorted? - Roland

4 Answers

137
votes

The customary usage order of the modifiers is mentioned in the Java Language Specification (and not the Java Virtual Machine Specification) e.g. for class modifiers you will find the following definition (extract):

ClassModifiers:
    ClassModifier
    ClassModifiers ClassModifier

ClassModifier: one of
    Annotation public protected private
    abstract static final strictfp

[....]

If two or more (distinct) class modifiers appear in a class declaration, then it is customary, though not required, that they appear in the order consistent with that shown above in the production for ClassModifier. (small text at the bottom of the paragraph!)

You will find this sentence at several other places where the usage of modifiers is specified, e.g. here for field modifiers.

Update: I replaced "specified/recommended" with "customary" to make this an acceptable answer. Take this into account if you read the comments ;-) (thanks @EJP to make this clear) - Nevertheless I would recommend to use the customary order.

Google also recommends using the customary order mentioned in the Java spec.

public / protected / private 
abstract 
static 
final 
transient 
volatile 
synchronized 
native 
strictfp

Update: There is a new "Java Style Guidelines" initiative in place for projects in the OpenJDK community. It also has a recommendation for a modifier order and also includes the new default modifier of Java 8.

public / private / protected
abstract
static
final
transient
volatile
**default**
synchronized
native
strictfp
29
votes

It is reasonable to use the order according to the Java Virtual Machine Specification, Table 4.4

  • public
  • protected
  • private
  • abstract
  • default
  • static
  • final
  • transient
  • volatile
  • synchronized
  • native
  • strictfp
7
votes

Based on their int values.

Modifier (Java Platform SE 8 )

  • 1 : public
  • 2 : private
  • 4 : protected
  • 8 : static
  • 16 : final
  • 32 : synchronized
  • 64 : volatile
  • 128 : transient
  • 256 : native
  • 512 : interface
  • 1024 : abstract
  • 2048 : strictfp
0
votes

I use two rules to remember the modifier sequence, but doesn't include the strictfp, as it is never used by me. FYI.

  1. synchronized native are least priority people.

  2. PPP AS FTV: PPP {noise sound} AS {watching} FTV {France TV}.

:)