700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java static null 我们可以在Java中使用null对象调用静态方法吗?如果是这样 怎么样?...

java static null 我们可以在Java中使用null对象调用静态方法吗?如果是这样 怎么样?...

时间:2018-07-13 22:01:35

相关推荐

java static null 我们可以在Java中使用null对象调用静态方法吗?如果是这样 怎么样?...

Since static methods can be called directly from the class (i.e. ClassName.methodName), why it is required to call a static method with the object of the class?

If someone knows then, elaborate with example.

public static void methodA(){

}

解决方案

The following code contains an example, in which a static method is called via a null reference.

public class Test {

public static void main(String... args) {

Test test = null;

test.greeting(); // call with null reference

}

public static void greeting() {

System.out.println("Hello World");

}

}

Because Test::greeting is a static method, the expression test.greeting() is identical to Test.greeting(). For that reason, there is no NullPointerException thrown at runtime.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。