I was just discussing about calling static methods using class name with my friend and tried out this code and expected it to throw NPE at runtime.but as it turn out it dint
It works because what matters is the compile-time type of the o
field. The compiler will compile o.method()
into the same byte code as One.method()
.
In particular, if you had a class Two
that extends One
, and both declare a static void method()
, then
One x = new Two();
x.method(); // calls One.method(), not Two.method()
Good for obfuscation purposes, less good for maintainability...