Java Modifiers
Java Modifiers Matrix Table
There are few sets of modifers in Java Langauge: Class Modifiers, Field Modifiers, Method Modifiers, Constructor Modifiers, Interface Modifiers, and Member Class (inner class) Modifiers. The following table shows which modifier to which element can be applied.
| element | Field | Method | Constructor | Class | Interface | ||
|---|---|---|---|---|---|---|---|
| modifier |
top level (outer) |
nested (inner) |
top level (outer) |
nested (inner) | |||
| abstract | no | yes | no | yes | yes | yes | yes |
| final | yes | yes | no | yes | yes | no | no |
| native | no | yes | no | no | no | no | no |
| private | yes | yes | yes | no | yes | no | yes |
| protected | yes | yes | yes | no | yes | no | yes |
| public | yes | yes | yes | yes | yes | yes | yes |
| static | yes | yes | no | no | yes | no | yes |
| synchronized | no | yes | no | no | no | no | no |
| transient | yes | no | no | no | no | no | no |
| volatile | yes | no | no | no | no | no | no |
| strictfp | no | yes | no | yes | yes | yes | yes |
Rules
- The same modifier can not appear more than once in any declarations.
Rules for Class Modifiers
-
The access modifier
publicpertains only to top level classes and to member classes; -
The access modifiers
protectedandprivatepertain only to member classes within a directly enclosing class declaration; -
The access modifier
staticpertains only to member classes; -
A class that contains
abstractmethod(s) must be declaredabstract; -
The class cannot be declared
abstractandfinalsimultaneously; -
The class can be declared
finalif its definition is complete and no subclasses are desired or required; -
The effect of the
strictfpmodifier is to make allfloatordoubleexpressions within the class declaration be explicitly FP-strict;
Rules for Constructor Modifiers
-
A constructor cannot be
abstract,static,final,native,strictfp, orsynchronized; -
The constructor declaration can contain only one of the access modifiers
public,protected, andprivate; -
If no access modifier is specified for the constructor of an enum type, the
constructor is
private;
Rules for Method Modifiers
-
The method declaration can contain only one of the access modifiers
public,protectedand private; -
The
abstractmethods cannot be declaredprivate,static,final,native,strictfporsynchronized; -
The methods cannot be declared
nativeandstrictfpsimultaneously; -
The
abstractandnativemethods have no body;
Rules for Field Modifiers
-
finalfields cannot bevolatile;
Rules for Interface Modifiers
-
The access modifier
publicpertains only to top level interfaces and to member interfaces; -
The access modifiers
protectedandprivatepertain only to member interfaces within a directly enclosing class declaration; -
The access modifier
staticpertains only to member interfaces; -
Every interface is implicitly
abstract; The top level interface ispublicandabstract; -
The effect of the
strictfpmodifier is to make allfloatordoubleexpressions within the interface declaration be explicitly FP-strict;