Java Reference
In-Depth Information
}
}
8.4.3.2. static Methods
A method that is declared
static
is called a
class method
.
It is a compile-time error to use the name of a type parameter of any surrounding declara-
tion in the header or body of a class method.
A class method is always invoked without reference to a particular object. It is a compile-
A method that is not declared
static
is called an
instance method
, and sometimes called a
non-
static
method.
An instance method is always invoked with respect to an object, which becomes the current
object to which the keywords
this
and
super
refer during execution of the method body.
8.4.3.3. final Methods
A method can be declared
final
to prevent subclasses from overriding or hiding it.
It is a compile-time error to attempt to override or hide a
final
method.
as if they are
final
, since it is impossible to override them.
At run time, a machine-code generator or optimizer can “inline” the body of a
final
method, replacing an invocation of the method with the code in its body. The inlining
process must preserve the semantics of the method invocation. In particular, if the tar-
get of an instance method invocation is
null
, then a
NullPointerException
must be thrown
even if the method is inlined. A Java compiler must ensure that the exception will be
thrown at the correct point, so that the actual arguments to the method will be seen to
have been evaluated in the correct order prior to the method invocation.
Consider the example:
final class Point {
int x, y;
void move(int dx, int dy) { x += dx; y += dy; }
}
class Test {
public static void main(String[] args) {