在實例化對象前,若是沒有加載過相應的類信息,那麼會首先加載類,而後才實例化對象。函數
在這種狀況下,順序爲:測試
a. 加載類spa
b. 建立對象code
public class Father { Another a = new Another("father 普通成員"); static Another a1 = new Another("father 靜臺成員"); static { Another a2 = new Another("father static block"); } public Father(){ System.out.println("執行father構造"); } }
子類:對象
public class Son extends Father{ static Another aa1 = new Another("son 靜態成員"); Another a2 = new Another("son 普通成員"); static{ Another a2 = new Another("son 靜態代碼塊"); } public Son(){ System.out.println("執行Son構造函數"); } }
測試類:blog
public class Test { static { System.out.println("靜態代碼塊執行"); } public static void main(String[] args) { Son tn = new Son(); } }
輸出爲:內存
靜態代碼塊執行
father 靜臺成員
father static block
son 靜態成員
son 靜態代碼塊
father 普通成員
執行father構造
son 普通成員
執行Son構造函數
參考:https://www.zhihu.com/question/49196023/answer/114859346io