Sunday, 11 August 2013

Resolving static method at compile-time

Resolving static method at compile-time

It's a common fact that Java compiler (almost) always resolves static
methods at compile time. For example:
public class Super {
static void someMethod() {
// Do something...
}
}
public class Derived extends Super {
// Some other methods, excluding someMethod
}
Test code:
Derived derived = new Derived();
derived.someMethod();
This should call Super.someMethod(), right? And it should be resolved at
compile-time, so that javac would generate invokestatic Super.someMethod,
but I've seen that it generates invokestatic Derived.someMethod. Why is it
doing so? And is there a way to somehow change this behavior?
Please correct me, if I am wrong.

No comments:

Post a Comment