What happens when a static method is invoked using a null object reference?
问题 public class CallingStaticMethod { public static void method() { System.out.println("I am in method"); } public static void main(String[] args) { CallingStaticMethod csm = null; csm.method(); } } Can someone explain how the static method is invoked in the above code? 回答1: It's been optimized away by the compiler, simply because having an instance of the class is not necessary. The compiler basically replaces csm.method(); by CallingStaticMethod.method(); It's in general also a good practice