why doesn't this code throw NullPointerException

后端 未结 5 1316
攒了一身酷
攒了一身酷 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:19

    If you would have opened the code in your Development Environment e.g (Eclipse), instead of fooling people by showing the code here, which does provide code formating for static methods in italic style, then you would have seen that checkstyle claims about "Do not call a static method on an instance".

    So it should be

    One.method()
    

    instead of

    o.method()
    

    Then it is clear why it does not crash!

提交回复
热议问题