Modifier Keyword order in Java
Asked Answered
D

9

67

Every time I write a 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]?

Dichromic answered 24/4, 2012 at 13:38 Comment(1)
Sounds like a question of mine that got booted to programmers.stackexchange.com... But personally I use [public/private/protected] [final/abstract/static] [synchronized] [type] func()...Lim
H
79

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

Hashimoto answered 24/4, 2012 at 13:40 Comment(4)
@YoushaAleayoub On provided JLS sections, It does not say anything about order restrictions, something like It is compile time error, if you do not follow above order. :)Hashimoto
I think the order for static and abstract are swapped according to docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.3. Going to edit it..Jefferey
@11thHourWorker: Considering that static and abstract cannot appear at the same time for one declaration their mutual order is irrelevant.Arris
@Joey, class Outer { static abstract class Inner {} }Rejection
A
56

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
Africanist answered 19/7, 2013 at 19:41 Comment(2)
1.-3. can never occur together so they should be tied to first place.Crumble
@KonradHöffner, No, it's much clearer this way. Despite the technicality, I was hoping for a list where I could visually just pick and choose along a single dimension. The fact that you cannot have, say, a public protected something is moot. This is about overall ordering, not about what's allowed together. Sensible pairing is for the BNF (and the like) of languages, not this question.Horrified
G
9

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

Girasol answered 24/5, 2013 at 10:10 Comment(0)
O
3

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;
}
Oswin answered 24/4, 2012 at 13:43 Comment(0)
K
2

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

Known answered 24/4, 2012 at 13:40 Comment(3)
Yes, but you can't deny that public static final is more common than public final static. There are some unwritten conventions.Merla
Indeed, there are some pretty clear conventions in Javaland, for instance naming methods with infixCaps style. It is worth learning, and following them.Oswin
A lot of code is developed in teams. So, stick to the official convention.Bugaboo
M
1

Like this:

public static final synchronized void calculate()

Merla answered 24/4, 2012 at 13:40 Comment(1)
public static final synchronized void calculate() ;-)Thermosphere
A
0

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.

Anastassia answered 24/4, 2012 at 13:44 Comment(1)
This does not re-order the modifiers AFAIK. See bugs.eclipse.org/bugs/show_bug.cgi?id=322494Unheardof
A
0

If you use SonarQube static analysis tool, or choose to follow its conventions, it should be:

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

See https://rules.sonarsource.com/java/tag/convention/RSPEC-1124

Animadversion answered 18/7, 2022 at 12:50 Comment(0)
A
-1

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

Anabiosis answered 24/4, 2012 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.