The language document (I'm looking at the beta draft of July 29, 1995
9:42 am, Section 5.6.2 Method Modifiers on page 32) says:
A static method is implicitly final, so no overriding occurs on static
methods
which does seem to be true, but if I try to make a static method explicitly
final, I find I can't declare another static method with the same name in a
subclass. For example:
% cat java/Base.java
public class Base {
public static final String method() {
return "Base.method()";
}
}
% cat java/Derived.java
public class Derived extends Base {
public static String method() {
return "Derived.method";
}
}
% ..../jdk.prebeta.1/bin/javac .... ./java/Derived.java
./java/Derived.java:2: Final methods can't be overriden. Method java.lang.String method() is final in class Base.
public static String method() {
^
1 error
I guess you could say this is a bug in the documentation, but I think the
documentation is correct, in terms of the effect of declaring a method
static keeping it from being overridden by subclasses. I think the bug is
in the compiler, which should allow static methods to be explicitly
declared final.
... peter 10/16/95
9:42 am, Section 5.6.2 Method Modifiers on page 32) says:
A static method is implicitly final, so no overriding occurs on static
methods
which does seem to be true, but if I try to make a static method explicitly
final, I find I can't declare another static method with the same name in a
subclass. For example:
% cat java/Base.java
public class Base {
public static final String method() {
return "Base.method()";
}
}
% cat java/Derived.java
public class Derived extends Base {
public static String method() {
return "Derived.method";
}
}
% ..../jdk.prebeta.1/bin/javac .... ./java/Derived.java
./java/Derived.java:2: Final methods can't be overriden. Method java.lang.String method() is final in class Base.
public static String method() {
^
1 error
I guess you could say this is a bug in the documentation, but I think the
documentation is correct, in terms of the effect of declaring a method
static keeping it from being overridden by subclasses. I think the bug is
in the compiler, which should allow static methods to be explicitly
declared final.
... peter 10/16/95