Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifier Keyword order in Java

Every time I write method in Java with more keywords than public void, every time I write it another way. Sometimes "static public void" sometimes "public static void" etc.

What is the best order (best practices) for these keywords?

[abstract/static] [final] [synchronized] [public/private/protected] [result_type] ???

like image 463
user1227115 Avatar asked Apr 24 '12 13:04

user1227115


People also ask

What is the order of access modifiers in Java?

There are four access modifiers used in java. They are public, private, protected, no modifer (declaring without an access modifer).

What does modifier mean in Java?

Mar 29, 2021. Access modifiers are object-oriented programming that is used to set the accessibility of classes, constructors, methods, and other members of Java. Using the access modifiers we can set the scope or accessibility of these classes, methods, constructors, and other members.

Which keywords in Java are access modifiers?

Java access modifiers are used to provide access control in java. Java provides access control through three keywords - private, protected and public. We are not required to use these access modifiers always, so we have another one namely “default access”, “package-private” or “no modifier”.


2 Answers

In theory it does not matter if you say public static final or final static public, but if you follow the usual convention, other people will able to read your code more easily. Here is the preferred order:

[ public | protected | private ]

static

abstract

synchronized

[ transient | volatile ]

final

native

strictfp

[ int | long | String | class | enum | interface etc. ]

like image 140
Nandkumar Tekale Avatar answered Sep 19 '22 11:09

Nandkumar Tekale


Checkstyle (which implements the suggestions of the Java Language Specifications sections, 8.1.1, 8.3.1, and 8.4.3) says:

  1. public
  2. protected
  3. private
  4. abstract
  5. default
  6. static
  7. final
  8. transient
  9. volatile
  10. synchronized
  11. native
  12. strictfp
like image 30
Drew Stephens Avatar answered Sep 20 '22 11:09

Drew Stephens