Modifier Keyword order in Java

JavaSyntaxCoding StyleAccess Modifiers

Java Problem Overview


Every time I write method in Java with more keywords than public void, every time I write it another way. Sometimes "static public void" sometimes "public static void" etc.

What is the best order (best practices) for these keywords?

[abstract/static] [final] [synchronized] [public/private/protected] [result_type] ???

Java Solutions


Solution 1 - Java

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 - Java

Checkstyle (which implements the suggestions of the Java Language Specifications sections, 8.1.1, 8.3.1, and 8.4.3) says:

  1. public
  2. protected
  3. private
  4. abstract
  5. default
  6. static
  7. final
  8. transient
  9. volatile
  10. synchronized
  11. native
  12. strictfp

Solution 3 - Java

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 - Java

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 - Java

The best order is the one that the rest of your code uses.

Solution 6 - Java

Like this:

public static final synchronized void calculate()

Solution 7 - Java

Yes, there is a standard ordering.

If you use an IDE, you can set it up to format your code for you, i.e. in Eclipse in Preferences -> Java -> Editor -> Save Actions you can check the box "Format source code"

Then you don't have to worry about it any more. It will be done automatically whenever the file is saved and if your whole project uses this, then for the whole project has code that is formatted in the same way.

Solution 8 - Java

This is my personal choice

public static final void method() { }
public void method() { }
public abstract void method() { }

this seems in line too with the java documentation

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1227115View Question on Stackoverflow
Solution 1 - JavaNandkumar TekaleView Answer on Stackoverflow
Solution 2 - JavaDrew StephensView Answer on Stackoverflow
Solution 3 - JavaFrVaBeView Answer on Stackoverflow
Solution 4 - JavaewernliView Answer on Stackoverflow
Solution 5 - JavaMatt BallView Answer on Stackoverflow
Solution 6 - JavaPetar MinchevView Answer on Stackoverflow
Solution 7 - JavaAlan EscreetView Answer on Stackoverflow
Solution 8 - JavakabalView Answer on Stackoverflow