What is a reasonable order of Java modifiers (abstract, final, public, static, etc.)?
Asked Answered
D

4

150

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.

Downwind answered 24/5, 2013 at 9:3 Comment(6)
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.Variolous
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.Unabridged
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.Variolous
@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!Unabridged
I finally suggested an update of the Java Code Conventions in the OpenJDK discuss mailing list and did a short blog post about it.Unabridged
Can Eclipse sort the modifiers automatically? Or at least give a warning if they are unsorted?Lockyer
U
166

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
Unabridged answered 24/5, 2013 at 9:45 Comment(15)
No. Describing this as a 'recommendation' is a category mistake. It isn't a 'recommendation' in any way shape or form. It is a specification of the grammar that the compiler will accept, in a form that was convenient to the people who wrote the grammar. Completely different thing. The syntax specified here means that the order is completely immaterial. And why not the JVM Specification?Variolous
The question was "What is the recommended order of Java modifiers?" and this questions is answerd in the Language Specification (and as the order is not required I would say it is recommended in the specification). The JVM specification is about the class file format and not the Java Source code. - Indeed the order does not matter but if you follow a recommendation your code will be more readable to others. So I think this is a reasonable question where a precise answer can be given. By the way - static code analyzer (e.g. SONAR) will complane about the wrong order.Unabridged
The question is not answered in the JLS. The JLS says 'one of'. The JLS is not a place for style recommendations. It is a place where syntax and semantics are specified. If they wanted to only allow one order, this is where they would have done it. They didn't. They deliberately allowed any order. The word 'recommend' does not appear. If SONAR or any other 'static code analyzer' complains about the wrong order, it doesn't implement the Java grammar quoted here, and so isn't a Java code analyzer at all: it is an analyzer for an unspecified unsourced subset of Java which has no real existence.Variolous
The sentence 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. IS PART OF THE SPECIFICATION!Unabridged
'Customary' isn't the same as 'recommended', let alone 'specified/recommended', whatever that means; and 'not required' gives the entire show away. And don't write in capitals here, it is widely considered as shouting.Variolous
In this case I wanted to shout but I calmed down already - so sorry about that ;-) - I am also not native - so you are probably right on language interpretation. What I just wanted to say with my answer is that there is a very official place where the order of modifiers in Java Source Code is handled - and I was lucky to find this when I asked the same question to myself. IMHO there is no better place to find the answer than in the Java Language Specification.Unabridged
let us continue this discussion in chatUnabridged
@EJP You have a strong opinion (~ no recommended order) on that topic. Would you be so kind to provide this as an answer? I am curious about the voting.Unabridged
I'm going to remember this order as BASF, which should be PASF: public|private|protected abstract static finalElector
@Unabridged - grammar correction -- "customary" (adjective) not "custom" (noun), please. While referring to this usage as "a custom" would technically be correct English, one normally wouldn't use "custom" here.Doura
See: Google recommends using the customary order mentioned in the Java specDoura
How about default in Java 8?Osteoclasis
@FranklinYu The modifier order for interface methods listed in the Java Language Specification is public - one of abstract | default | static - strictfp; have a look at my updated answerUnabridged
@Doura your link is brokenValenti
@Doura Which link are you talking about. I checked all links and could not find any broken one? Probably I missed something...Unabridged
D
38

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
Downwind answered 24/5, 2013 at 9:3 Comment(4)
+1 you see often: "public abstract", "private static final" etc. Some IDEs (e.g. NetBeans) even have some shortcuts, e.g. "psf" or "Psf"Gemination
You can find the recommended order in the Java Language Specification (and not the Java Virtual Machine Specification!). Have a look at my answer.Unabridged
@Unabridged You can find no such thing. Don't confuse language grammars with style recommendations.Variolous
@Puce: some IDEs (e.g. IntelliJ IDEA) even can correctly sort the modifiers.Polack
F
7

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
Forlorn answered 6/10, 2015 at 9:35 Comment(2)
this answer does not include the keyword "default"Forlorn
What do their int values have to do with it?Variolous
S
0

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

:)

Shelves answered 23/1, 2015 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.