Modifiers, which are Java keywords, may be applied to classes, interfaces, constructors, methods, and data members.
Table 8-1 lists the Java modifiers and their applicability. Note that private and protected classes are allowed, but only as inner or nested classes. Reference Chapter 21 for accessibility details relative to Java 9.
Table 8-1. Java modifiers
Modifier
Class
Interface
Constructor
Method
Data member
Access modifiers
package-private
Yes
Yes
Yes
Yes
Yes
private
No
No
Yes
Yes
Yes
protected
No
No
Yes
Yes
Yes
public
Yes
Yes
Yes
Yes
Yes
Other modifiers
abstract
Yes
Yes
No
Yes
No
final
Yes
No
No
Yes
Yes
native
No
No
No
Yes
No
strictfp
Yes
Yes
No
Yes
No
static
No
No
No
Yes
Yes
synchronized
No
No
No
Yes
No
transient
No
No
No
No
Yes
volatile
No
No
No
No
Yes
Inner classes may also use the private or protected access modifiers. Local variables may only use one modifier: final.
Access Modifiers
Access modifiers define the access privileges of classes, interfaces, constructors, methods, and data members. Access modifiers consist ofpublic, private, and protected. If no modifier is present, the default access of package-private is used.
Table 8-2 provides details on visibility when access modifiers are used.
Table 8-2. Access modifiers and their visibility
Modifier
Visibility
package-private
The default package-private limits access from within the package.
private
The private method is accessible from within its class.
The private data member is accessible from within its class or interface (Java 9). It can be indirectly accessed through methods (i.e., getter and setter methods).
protected
The protected method is accessible from within its package, and also from outside its package by subclasses of the class containing the method.
The protected data member is accessible within its package, and also from outside its package by subclasses of the class containing the data member.
public
The public modifier allows access from anywhere, even outside of the package in which it was declared. Note that interfaces are public by default.
Other (Nonaccess) Modifiers
Table 8-3 contains the nonaccess Java modifiers and their usage.
Table 8-3. Nonaccess Java modifiers
Modifier
Usage
abstract
Anabstract class is a class that is declared with the keyword abstract. It cannot be simultaneously declared with final. Interfaces are abstract by default and do not have to be declared abstract.
An abstract method is a method that contains only a signature and no body. If at least one method in a class is abstract, then the enclosing class is abstract. It cannot be declared final, native, private, static, or synchronized.
default
Adefault method, a.k.a. defender method, allows for the creation of a default method implementation in an interface.
final
A final class cannot be extended.
A final method cannot be overridden.
A final data member is initialized only once and cannot be changed. A data member that is declared static final is set at compile time and cannot be changed.
native
Anative method is used to merge other programming languages such as C and C++ code into a Java program. It contains only a signature and no body. It cannot be used simultaneously withstrictfp.
static
Bothstatic methods and static variables are accessed through the class name. They are used for the whole class and all instantiations from that class.
A static data member is accessed through the class name. Only one static data member exists, no matter how many instances of the class exist.
strictfp
A strictfp class will follow the IEEE 754-1985 floating-point specification for all of its floating-point operations.
A strictfp method has all expressions in the method as FP-strict. Methods within interfaces cannot be declared strictfp. It cannot be used simultaneously with the native modifier.
synchronized
Asynchronized method allows only one thread to execute the method block at a time, making it thread safe. Statements can also be synchronized.
transient
Atransient data member is not serialized when the class is serialized. It is not part of the persistent state of an object.
volatile
Avolatile data member informs a thread, both to get the latest value for the variable (instead of using a cached copy) and to write all updates to the variable as they occur.
Modifiers Encoding
Modifiers applied to classes and members can be retrieved with Class.getModifiers() and Member.getModifiers(), respectively. The modifiers are encoded, and can be decoded with Modifier.toString(value):
// Modifiers used on the HashMap classClassc=newHashMap().getClass();Stringmodifiers=Modifier.toString(c.getModifiers());System.out.println("Class Modifier(s) = "+modifiers);$ClassModifier(s)=public// Modifiers used on the Hashmap isEmpty// member/methodMemberm=newHashMap().getClass().getDeclaredMethod("isEmpty");Stringmodifiers=Modifier.toString(m.getModifiers());System.out.println("Method Modifier(s) = "+modifiers);$MethodModifier(s)=public