package NowCoder; class Test { public static void hello() { System.out.println("hello"); } } public class MyApplication { public static void main(String[] args) { Test test=null; test.hello(); } }
結果
hello
解析:
靜態方法是屬於類的,與對象無關,只要是使用到了Test類,都會加載靜態hello方法!
空指針必須是去引用堆對象才會有空指針,而這個hello是static類型的,人家static的方法本身就沒有指針,所以當然不會有空指針。
引用類型 引用名=null,其實就是指該引用在堆中沒有對應的對象,但是編譯的時候還是能根據聲明找到其所屬的靜態類型。