Java modifiers syntax and format

From the official grammar of the Java Programming Language (simplified):

Modifier:
  Annotation | public | protected | private
  static | abstract | final | native | synchronized
  transient | volatile | strictfp

ClassOrInterfaceDeclaration:
        {Modifier} (ClassDeclaration | InterfaceDeclaration)

ClassBodyDeclaration:
        {Modifier} MethodOrFieldDecl

MethodOrFieldDecl:
        Type Identifier MethodOrFieldRest

So, for classes and interfaces, the modifiers must always appear before the class keyword, and in any order. E.g., final public class is valid, but class final is not. For methods and fields, it is the same, but the modifiers must appear before the type.

Leave a Comment