There is Why doesn't Java allow overriding of static methods?, which alleges that overriding static methods is not allowed in java. Yet it seems to work in OpenJDK:
Compiling these two classes works when not using @Override
, but
fails when doing so.
To reproduce this, the file Parent.java
looks like this.
public class Parent {
public static int getActivity() { return 1; }
}
and the file Child.java
like this:
public class Child extends Parent {
// @Override public static int getActivity() { return 2; } // fails
public static int getActivity() { return 2; } // works
public static void main(String ... args) {
System.out.println((new Child()).getActivity());
}
}
The error is when using @Override
is
$ javac Child.java
Child.java:3: error: method does not override or implement a method from a supertype
@Override public static int getActivity() { return 2; }
When removing the @Override
, the output is 2
, of the Child
method.
The javac is version javac 1.7.0_79
of the OpenJDK.
Where is the error? (the ideal would be to add @Override
to static
methods, but the answer that this is a bug in the OpenJDK
or my thinking would be good, too)