Java-靜態(tài)代碼塊,構(gòu)造代碼塊,構(gòu)造函數(shù)

靜態(tài)代碼塊:用staitc聲明,jvm加載類時執(zhí)行,僅執(zhí)行一次
構(gòu)造代碼塊:類中直接用{}定義,每一次創(chuàng)建對象時執(zhí)行。
執(zhí)行順序優(yōu)先級:靜態(tài)塊, main(),函數(shù),構(gòu)造塊,構(gòu)造方法。

構(gòu)造函數(shù)

public HelloA(){//構(gòu)造函數(shù)
    }

關(guān)于構(gòu)造函數(shù),以下幾點(diǎn)要注意:
1.對象一建立,就會調(diào)用與之相應(yīng)的構(gòu)造函數(shù),也就是說,不建立對象,構(gòu)造函數(shù)時不會運(yùn)行的。
2.構(gòu)造函數(shù)的作用是用于給對象進(jìn)行初始化。
3.一個對象建立,構(gòu)造函數(shù)只運(yùn)行一次,而一般方法可以被該對象調(diào)用多次。

構(gòu)造代碼塊

{//構(gòu)造代碼塊    
}

關(guān)于構(gòu)造代碼塊,以下幾點(diǎn)要注意:

  1. 構(gòu)造代碼塊的作用是給對象進(jìn)行初始化。
  2. 對象一建立就運(yùn)行構(gòu)造代碼塊了,而且優(yōu)先于構(gòu)造函數(shù)執(zhí)行。這里要強(qiáng)調(diào)一下,有對象建立,才會運(yùn)行構(gòu)造代碼塊,類不能調(diào)用構(gòu)造代碼塊的,而且構(gòu)造代碼塊與構(gòu)造函數(shù)的執(zhí)行順序是前者先于后者執(zhí)行。
  3. 構(gòu)造代碼塊與構(gòu)造函數(shù)的區(qū)別是:構(gòu)造代碼塊是給所有對象進(jìn)行統(tǒng)一初始化,而構(gòu)造函數(shù)是給對應(yīng)的對象初始化,因為構(gòu)造函數(shù)是可以多個的,運(yùn)行哪個構(gòu)造函數(shù)就會建立什么樣的對象,但無論建立哪個對象,都會先執(zhí)行相同的構(gòu)造代碼塊。也就是說,構(gòu)造代碼塊中定義的是不同對象共性的初始化內(nèi)容。

靜態(tài)代碼塊

static {//靜態(tài)代碼塊 
}

關(guān)于靜態(tài)代碼塊,要注意的是:

  1. 它是隨著類的加載而執(zhí)行,只執(zhí)行一次,并優(yōu)先于主函數(shù)。具體說,靜態(tài)代碼塊是由類調(diào)用的。類調(diào)用時,先執(zhí)行靜態(tài)代碼塊,然后才執(zhí)行主函數(shù)的。
  2. 靜態(tài)代碼塊其實就是給類初始化的,而構(gòu)造代碼塊是給對象初始化的。
  3. 靜態(tài)代碼塊中的變量是局部變量,與普通函數(shù)中的局部變量性質(zhì)沒有區(qū)別。
  4. 一個類中可以有多個靜態(tài)代碼塊
public class Test{
   staitc int cnt=6;
   static{
         cnt+=9;
   }
  public static void main(String[] args) {
         System.out.println(cnt);
  }
   static{
         cnt/=3;
   }
}
運(yùn)行結(jié)果:
5

例子1:

public class HelloA {
    public HelloA(){//構(gòu)造函數(shù)
        System.out.println("A的構(gòu)造函數(shù)");   
    }
    {//構(gòu)造代碼塊
        System.out.println("A的構(gòu)造代碼塊");  
    }
    static {//靜態(tài)代碼塊
        System.out.println("A的靜態(tài)代碼塊");      
    }
    public static void main(String[] args) {
    }
}
運(yùn)行結(jié)果:
A的靜態(tài)代碼塊

例子2:

public class HelloA {
    public HelloA(){//構(gòu)造函數(shù)
        System.out.println("A的構(gòu)造函數(shù)");   
    }
    {//構(gòu)造代碼塊
        System.out.println("A的構(gòu)造代碼塊");  
    }
    static {//靜態(tài)代碼塊
        System.out.println("A的靜態(tài)代碼塊");      
    }
    public static void main(String[] args) {
        HelloA a=new HelloA();  
    }
}
運(yùn)行結(jié)果:
A的靜態(tài)代碼塊
A的構(gòu)造代碼塊
A的構(gòu)造函數(shù)

例子3:

public class HelloA {
    public HelloA(){//構(gòu)造函數(shù)
        System.out.println("A的構(gòu)造函數(shù)");   
    }
    {//構(gòu)造代碼塊
        System.out.println("A的構(gòu)造代碼塊");  
    }
    static {//靜態(tài)代碼塊
        System.out.println("A的靜態(tài)代碼塊");      
    }
    public static void main(String[] args) {
        HelloA a=new HelloA();
        HelloA b=new HelloA();
    }
}
運(yùn)行結(jié)果:
A的靜態(tài)代碼塊
A的構(gòu)造代碼塊
A的構(gòu)造函數(shù)
A的構(gòu)造代碼塊
A的構(gòu)造函數(shù)

對于一個類而言,按照如下順序執(zhí)行:

  1. 執(zhí)行靜態(tài)代碼塊
  2. 執(zhí)行構(gòu)造代碼塊
  3. 執(zhí)行構(gòu)造函數(shù)

例子4:

public class HelloA {
    public HelloA(){//構(gòu)造函數(shù)
        System.out.println("A的構(gòu)造函數(shù)");   
    }
    {//構(gòu)造代碼塊
        System.out.println("A的構(gòu)造代碼塊");  
    }
    static {//靜態(tài)代碼塊
        System.out.println("A的靜態(tài)代碼塊");      
    }
}
public class HelloB extends HelloA{
    public HelloB(){//構(gòu)造函數(shù)
        System.out.println("B的構(gòu)造函數(shù)");   
    }
    {//構(gòu)造代碼塊
        System.out.println("B的構(gòu)造代碼塊");  
    }
    static {//靜態(tài)代碼塊
        System.out.println("B的靜態(tài)代碼塊");      
    }
    public static void main(String[] args) {
        HelloB b=new HelloB();      
    }
}
運(yùn)行結(jié)果:
A的靜態(tài)代碼塊
B的靜態(tài)代碼塊
A的構(gòu)造代碼塊
A的構(gòu)造函數(shù)
B的構(gòu)造代碼塊
B的構(gòu)造函數(shù)

當(dāng)涉及到繼承時,按照如下順序執(zhí)行:

  1. 執(zhí)行父類的靜態(tài)代碼塊,并初始化父類靜態(tài)成員變量
  2. 執(zhí)行子類的靜態(tài)代碼塊,并初始化子類靜態(tài)成員變量
  3. 執(zhí)行父類的構(gòu)造代碼塊,執(zhí)行父類的構(gòu)造函數(shù),并初始化父類普通成員變量
  4. 執(zhí)行子類的構(gòu)造代碼塊, 執(zhí)行子類的構(gòu)造函數(shù),并初始化子類普通成員變量
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容