Book Home Java Enterprise in a Nutshell Search this book

3.14. 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. This chapter has introduced the public, protected, and private access modifiers, as well as the abstract, final, and static modifiers. In addition to these six, Java defines five other less commonly used modifiers. Table 3-2 lists the Java modifiers, explains what types of Java constructs they can modify, and explains what they do.

Table 3-2. Java Modifiers

ModifierUsed onMeaning
abstractclass

The class contains unimplemented methods and cannot be instantiated.

interface

All interfaces are abstract. The modifier is optional in interface declarations.

abstractmethod

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.

finalclass

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 ( Java 1.1 and later). Useful with local classes.

nativemethod

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.

privatemember

The member is accessible only within the class that defines it.

protectedmember

The member is accessible only within the package in which it is defined and within subclasses.

publicclass

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.

strictfpclass

All methods of the class are implicitly strictfp ( Java 1.2 and later).

strictfpmethod

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 ( Java 1.2 and later). This modifier is rarely used.

staticclass

An inner class declared static is a top-level class, not associated with a member of the containing class (Java 1.1 and later).

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.

synchronizedmethod

The method makes non-atomic 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.

transientfield

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.

volatilefield

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.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.