javamethodsmodifiers

What are the maximum number of modifiers, a java method can contain?


There are several modifiers used before declaring a java method such as public, static, synchronized etc.

I just want to know the maximum numbers of modifiers or all the combination of modifiers a java method can contain.


Solution

  • See the Java language spec, chapter 8.4:

    MethodDeclaration:
      {MethodModifier} MethodHeader MethodBody
    

    and:

     MethodModifier:
     (one of) 
     Annotation public protected private 
     abstract static final synchronized native strictfp
    

    You can't mix:

    Taking all of that together (thanks to user Andreas for the excellent wording):

    Using regex syntax, we get to:

     [ public | protected | private] static final synchronized [native | strictfp]
    

    So, the maximum number is 5; and 6 different combinations of those 5 keywords.