why doesn't this code throw NullPointerException

后端 未结 5 1431
攒了一身酷
攒了一身酷 2021-01-11 12:42

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

5条回答
  •  我在风中等你
    2021-01-11 13:24

    Because you declare static One o; outside the main function. You can try to declare it inside the main function, it cannot even be compiled...

    Or you can declare it as One o = null in main, then it will be compiled but it's the same as One.method()

提交回复
热议问题