3.11. Modifier Summary
As we've seen, classes, interfaces, and their members can be declared with one or more modifiers keywords such as public , static , and final . Table 3-2 lists the Java modifiers, explains what types of Java constructs they can modify, and explains what they do. See also Section 3.1 and Section 3.2.1 earlier in this chapter, as well as Section 2.6.2 in Chapter 2.
Modifier | Used on | Meaning |
---|---|---|
abstract | Class | The class contains unimplemented methods and cannot be instantiated . |
Interface | All interfaces are abstract . The modifier is optional in interface declarations. | |
abstract | Method | No body is provided for the method; it is provided by a subclass. The signature is followed by a semicolon. The enclosing class must also be abstract . |
final | Class | The class cannot be subclassed. |
Method | The method cannot be overridden (and is not subject to dynamic method lookup). | |
Field | The field cannot have its value changed. static final fields are compile-time constants. | |
Variable | A local variable, method parameter, or exception parameter cannot have its value changed. Useful with local classes. | |
native | Method | The method is implemented in some platform-dependent way (often in C). No body is provided; the signature is followed by a semicolon. |
None (package) | Class | A non- public class is accessible only in its package. |
Interface | A non- public interface is accessible only in its package. | |
Member | A member that is not private , protected , or public has package visibility and is accessible only within its package. | |
private | Member | The member is accessible only within the class that defines it. |
protected | Member | The member is accessible only within the package in which it is defined and within subclasses. |
public | Class | The class is accessible anywhere its package is. |
Interface | The interface is accessible anywhere its package is. | |
Member | The member is accessible anywhere its class is. | |
strictfp | Class | All methods of the class are implicitly strictfp . |
strictfp | Method | All floating-point computation done by the method must be performed in a way that strictly conforms to the IEEE 754 standard. In particular, all values, including intermediate results, must be expressed as IEEE float or double values and cannot take advantage of any extra precision or range offered by native platform floating-point formats or hardware. This modifier is rarely used. |
static | Class | An inner class declared static is a top-level class, not associated with a member of the containing class. |
Method | A static method is a class method. It is not passed an implicit this object reference. It can be invoked through the class name . | |
Field | A static field is a class field. There is only one instance of the field, regardless of the number of class instances created. It can be accessed through the class name. | |
Initializer | The initializer is run when the class is loaded rather than when an instance is created. | |
synchronized | Method | The method makes nonatomic modifications to the class or instance, so care must be taken to ensure that two threads cannot modify the class or instance at the same time. For a static method, a lock for the class is acquired before executing the method. For a non- static method, a lock for the specific object instance is acquired. |
TRansient | Field | The field is not part of the persistent state of the object and should not be serialized with the object. Used with object serialization; see java.io.ObjectOutputStream . |
volatile | Field | The field can be accessed by unsynchronized threads, so certain optimizations must not be performed on it. This modifier can sometimes be used as an alternative to synchronized . This modifier is very rarely used. |