The Java programming language has 50 reserved words, which have predefined meaning in the language. Several of these keywords can be prepended to the declaration of a class, method or variable and are known as modifiers.
public class HelloWorld
{
public static void main(String[] args)
{
System.out.print("Hello, World");
System.out.println();
}
}
This “Hello world” example uses four reserved words
(i.e. public
, class
, static
,
void
). Of those reserved words, only public
and static
act as modifiers. Although, every Java
application must contain a main
method, whose signature is
shown in the above example, the word main
is not a reserved
word.
Learning how and when to use each modifier can be especially difficult because their definitions are often littered throughout introductory texts. Both as a reference, and in order to facilitate learning, this article will cover each modifier on one page.
There is a total of 11 modifiers in the Java programming language.
The most common modifiers in an introductory context are
abstract
, final
, private
,
protected
, public
, and static
.
More advanced modifiers include native
,
synchronized
, transient
, volatile
and strictfp
.
Access modifiers help set the visibility and
accessibility of classes, methods and variables. The keywords
public
, protected
, and private
are access modifiers. A private
member is the most
restrictive access level and as such is only accessible in the class in
which it is defined. Therefore, private members are not inherited by a
subclass
Extension modifiers indicate whether a class can or
should be extended with a subclass. The keywords abstract
and final
are extension modifiers.
Any class may be declared with the abstract
,
final
, public
1 and
strictfp
modifiers. An abstract
class is to be
considered incomplete, such that subclasses can be created to complete
the implementation.2 A class can be declared
final
if its definition is complete and no subclasses are
desired or required, since the class can not be extended by another
class using the extend
keyword.
The remaining access and extension modifiers
(i.e. protected
, private
, and
static
), may only be declared in nested classes
(i.e. classes that are enclosed within another class declaration).
Certain modifiers can not be declared together. For example, a class
that is declared as abstract
can only be declared with
public
or protected
modifiers.
A method may be declared with the modifier names used with classes
and the native
and synchronized
modifiers.
That includes all modifiers except for volatile
and
strictfp
.
The abstract
modifier plays a similar role when declared
with methods. It means that the method is declared, but not yet
implemented. Abstract methods may only be composed inside of abstract
classes.
A compile-time error occurs if the same modifier appears more than
once in a class declaration. Although, the modifiers public
and static
can be written in either order, convention
dictates that the access modifier be written first
(i.e. public static
). After a access modifier, the
convention dictates the following order:
abstract static final strictfp
.
The access modifier public pertains only to top level classes and to member classes, not to local classes or anonymous classes.↩︎
If the intent is simply to prevent instantiation of a
class, the proper way to express this is to declare a constructor of no
arguments, make it private
, never invoke it, and declare no
other constructors.↩︎