剛開始學java吴裤,總是被各種莫名奇妙的執(zhí)行順序煩到窒息继蜡,比如以下一段經典案例:
public class StaticTest {
public static void main(String[] args) {
staticFunction();
}
static StaticTest st = new StaticTest();
static { //靜態(tài)代碼塊
System.out.println("1");
}
{ // 實例代碼塊
System.out.println("2");
}
StaticTest() { // 實例構造器
System.out.println("3");
System.out.println("a=" + a + ",b=" + b);
}
public static void staticFunction() { // 靜態(tài)方法
System.out.println("4");
}
int a = 110; // 實例變量
static int b = 112; // 靜態(tài)變量
}/* Output:
2
3
a=110,b=0
1
4
*///
網(wǎng)上查了一些資料,做了一個大綱方便自己理解苍蔬,算是筆記吧诱建。
參考文章:
深入理解Java對象的創(chuàng)建過程:類的初始化與實例化
https://blog.csdn.net/justloveyou_/article/details/72466416
JVM類生命周期概述:加載時機與加載過程
https://blog.csdn.net/justloveyou_/article/details/72466105