Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11631

How am I able to call a method on a null object?

$
0
0
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 initializedto 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”.


Viewing all articles
Browse latest Browse all 11631

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>