Chapter 8. Java Modifiers

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.

Table 8-1. Java modifiers
ModifierClassInterfaceConstructorMethodData 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 of public, 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
ModifierVisibility

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. 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
ModifierUsage

abstract

An abstract 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

A default method, a.k.a. defender method, allows for the creation of a default method implemntation 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

A native 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 with strictfp.

static

Both static 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

A synchronized method allows only one thread to execute the method block at a time, making it thread safe. Statements can also be synchronized.

transient

A transient data member is not serialized when the class is serialized. It is not part of the persistent state of an object.

volatile

A volatile 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.199.122