public class Null {
public static void greet() {
System.out.println("Hello world!");
}
public static void main(String[] args) {
((Null) null).greet();
}
}
program output: Hello world!.
I thought it would throw a NullPointerException.
Why is it happenning?
The reason is that
greet()is astaticmethod. References to static methods via variables do not result in dereferencing the pointer. The compiler should have warned you about this.If you remove the
staticmodifier then you will get thenpe