以下代码包含一个示例,其中通过
null引用调用了静态方法。
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"); }}由于
Test::greeting是静态方法,因此表达式
test.greeting()与相同
Test.greeting()。因此,
NullPointerException在运行时不会抛出异常。



