Are static methods in Java always resolved at compile time?
Yes, but if the static method has been removed by runtime the matching method in the base class will be called (name and signature must exactly match the original method from compile time, and the method must be accessible by JVM spec rules).
To clarify, consider calling code:
Derived.fn();
And the following called code:
class Base {
public static void fn() {
System.err.println("Base");
}
}
class Derived extends Base {
public static void fn() {
System.err.println("Derived");
}
}
Prints Derived.
Now, I compile everything. Then recompile just Derived changed to:
class Derived extends Base {
}
Prints Base.
Perhaps then I recompile just Derived changed to:
class Derived {
}
Throws an error.