public class JavaPuzzler {
public static void main(String[] args) {
JavaPuzzler javaPuzzler = null;
System.out.println(javaPuzzler.get());
}
private static String get(){
return "i am a java puzzler";
}
}
You might think that it should throw NullPointerException because the main method invokes get() method on local variable which is initialized
to null, and you can’t invoke a method on null.
But if you run this program, you will see that it prints “i am a java puzzler”.
In your code sample,
get()is a static member that belongs to the class, not to an instance. You do not need an instance in order to invoke the method.