Java Reference
In-Depth Information
final
A
final
method may not be overridden or hidden by a subclass, which makes
it amenable to compiler optimizations that are not possible for regular meth‐
ods. All
private
methods are implicitly
final
, as are all methods of any class
that is declared
final
.
a
x
nativ
The
native
modifier specifies that the method implementation is written in
some “native” language such as C and is provided externally to the Java pro‐
gram. Like
abstract
methods,
native
methods have no body: the curly braces
are replaced with a semicolon.
Implementing native Methods
When Java was first released,
native
methods were sometimes used for efficiency
reasons. That is almost never necessary today. Instead,
native
methods are used to
interface Java code to existing libraries written in C or C++.
native
methods are
implicitly platform-dependent, and the procedure for linking the implementation
with the Java class that declares the method is dependent on the implementation of
the Java virtual machine.
native
methods are not covered in this topic.
public, protected, private
These access modifiers specify whether and where a method can be used out‐
side of the class that defines it. These very important modifiers are explained in
Chapter 3
.
static
A method declared
static
is a
class method
associated with the class itself
rather than with an instance of the class (we cover this in more detail in
Chap‐
ter 3
).
strictfp
The
fp
in this awkwardly named, rarely used modifier stands for “floating
point.” Java normally takes advantage of any extended precision available to the
runtime platform's floating-point hardware. The use of this keyword forces Java
to strictly obey the standard while running the
strictfp
method and only per‐
form floating-point arithmetic using 32- or 64-bit floating-point formats, even
if this makes the results less accurate.
synchronized
The
synchronized
modifier makes a method threadsafe. Before a thread can
invoke a
synchronized
method, it must obtain a lock on the method's class (for
static
methods) or on the relevant instance of the class (for non-
static
methods). This prevents two threads from executing the method at the same
time.