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-
time error to attempt to reference the current object using the keyword this 15.8.3 ) or the
keyword super 15.11.2 ) .
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.
A private method and all methods declared immediately within a final class (§ 8.1.1.2 ) behave
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:
Click here to view code image
final class Point {
int x, y;
void move(int dx, int dy) { x += dx; y += dy; }
}
class Test {
public static void main(String[] args) {
Search WWH ::




Custom Search