There are four access modifiers used in java. They are public, private, protected, no modifer (declaring without an access modifer).
The four access modifiers in Java are public, protected, default, and private.
The customary usage order of the modifiers is mentioned in the Java Language Specification (and not the Java Virtual Machine Specification) e.g. for class modifiers you will find the following definition (extract):
ClassModifiers: ClassModifier ClassModifiers ClassModifier ClassModifier: one of Annotation public protected private abstract static final strictfp
[....]
If two or more (distinct) class modifiers appear in a class declaration, then it is customary, though not required, that they appear in the order consistent with that shown above in the production for ClassModifier. (small text at the bottom of the paragraph!)
You will find this sentence at several other places where the usage of modifiers is specified, e.g. here for field modifiers.
Update: I replaced "specified/recommended" with "customary" to make this an acceptable answer. Take this into account if you read the comments ;-) (thanks @EJP to make this clear) - Nevertheless I would recommend to use the customary order.
Google also recommends using the customary order mentioned in the Java spec.
public / protected / private
abstract
static
final
transient
volatile
synchronized
native
strictfp
Update: There is a new "Java Style Guidelines" initiative in place for projects in the OpenJDK community. It also has a recommendation for a modifier order and also includes the new default modifier of Java 8.
public / private / protected
abstract
static
final
transient
volatile
**default**
synchronized
native
strictfp
It is reasonable to use the order according to the Java Virtual Machine Specification, Table 4.4
Based on their int values.
Modifier (Java Platform SE 8 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With