我能夠調用迎接方法上null
,我在這裏期待NPE
因爲我們不能在null
從語言規範JLS-15.12
調用方法。方法調用工作正常 - 期待NPE
但是在這裏我得到hello world
public class Test{
public static void greet() {
System.out.println("Hello world!");
}
public static void main(String[] args) {
((Test) null).greet();
}
}
一些能幫助我,爲什麼我得到hello world
而不是NPE
這是一個'static'方法,所以你不會調用'null',因爲參考被忽略。 –