can we call any method on null object?

2019-02-18 09:49发布

is it possible?

Object obj=null;

obj.someMethod();

someMethod{/*some code here*/}

10条回答
我想做一个坏孩纸
2楼-- · 2019-02-18 10:42

This will always throw a NullPointerExcpetion unless someMethod is declared static. However, calling static methods on an instance is very bad practice.

查看更多
混吃等死
3楼-- · 2019-02-18 10:44

No we can't. it will throw NullPointerException as long as the method is not static. If method is static it will run.

Read this: null : Java Glossary

查看更多
Ridiculous、
4楼-- · 2019-02-18 10:48

yes you can call if the method is static because static method is bound at compile time and only type of variable is used for static binding not the value of object.

if you try the same for non static method,get ready to catch a null pointer exception.

查看更多
可以哭但决不认输i
5楼-- · 2019-02-18 10:51

No. In Java, null is not an object.

查看更多
登录 后发表回答