8.4.3
Method Modifiers
CLASSES
A method that is not declared
static
is called an
instance method,
and some
times 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 in a
final
class ( 8.1.2.2) are
implicitly
final
, because it is impossible to override them. It is permitted but not
required for the declarations of such methods to redundantly include the
final
keyword.
It is a compile time error for a
final
method to be declared
abstract
.
At run time, a machine code generator or optimizer can easily and safely
inline the body of a
final
method, replacing an invocation of the method with
the code in its body, as in 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) {
Point[] p = new Point[100];
for (int i = 0; i < p.length; i++) {
p[i] = new Point();
p[i].move(i, p.length 1 i);
}
}
}
Here, inlining the method
move
of class
Point
in method
main
would transform
the
for
loop to the form:
for (int i = 0; i < p.length; i++) {
p[i] = new Point();
Point pi = p[i];
pi.x += i;
pi.y += p.length 1 i;
}
The loop might then be subject to further optimizations.
160
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved