This is because for some strange reason, static
methods can actually be invoked through a reference. The static method to invoke is based on the type of the reference rather than the object, which means that allowing an instance method with the same signature as a static
method would create ambiguity as to what method to call.
For example, if this were allowed:
class A {
static void method() {
System.out.println("A");
}
}
class B extends A {
void method() {
System.out.println("B");
}
}
class Main {
public static void main(String[] args) {
A b = new B();
b.method();
}
}
What would happen? Should A
's implementation be called because b
is an A
reference? Or should B
's implementation be called because b
is a B
object?
Because both options are equally valid, allowing instance methods to "override" static
methods is disallowed, ensuring all method calls are valid.
Now, this isn't true for fields (both static and nonstatic) because fields can't be overridden in subclasses, only hidden. So the compiler could easily figure out what field you want based on the type of the reference.