- 過去に執筆した記事を日本語に書き写しました
- 翻訳機を使用しているため、誤字や不自然な表現があるかもしれません
はじめに
突然、フィールド宣言における修飾子(modifier)の宣言順序が気になり、調べてみました。
普段、私は習慣として
public static final String test = "hello";
と書いていたのですが、突然疑問に思うようになりました。
final static public String test = "hello";
こうしても良いのではないかと思い、一度調べてみました。
Java 17公式ドキュメント
8.4.3 Method Modifiers
MethodModifier(メソッド修飾子)のパートには、以下のように記載されています。
@Anotaition, 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.
複数の(異なる)メソッド修飾子がメソッド宣言に現れる場合、慣例として(必須ではありませんが)上記のMethodModifier規則で示された順序に従って記述することが望ましいです。
推奨事項
推奨される記述順序は以下の通りです:
- @ Annotation
- public
- protected
- private
- abstract
- static
- final
- synchronized
- native
- strictfp
上記の順序に従って記述することが望ましいです。