Java null object 能否调用 static 方法

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

1
2
3
4
5
6
7
8
9
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.

reference: