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.

Table 3-2. Java modifiers

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

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.