靜態(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)要注意:
- 構(gòu)造代碼塊的作用是給對象進(jìn)行初始化。
- 對象一建立就運(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í)行。
- 構(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)代碼塊,要注意的是:
- 它是隨著類的加載而執(zhí)行,只執(zhí)行一次,并優(yōu)先于主函數(shù)。具體說,靜態(tài)代碼塊是由類調(diào)用的。類調(diào)用時,先執(zhí)行靜態(tài)代碼塊,然后才執(zhí)行主函數(shù)的。
- 靜態(tài)代碼塊其實就是給類初始化的,而構(gòu)造代碼塊是給對象初始化的。
- 靜態(tài)代碼塊中的變量是局部變量,與普通函數(shù)中的局部變量性質(zhì)沒有區(qū)別。
- 一個類中可以有多個靜態(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í)行:
- 執(zhí)行靜態(tài)代碼塊
- 執(zhí)行構(gòu)造代碼塊
- 執(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í)行:
- 執(zhí)行父類的靜態(tài)代碼塊,并初始化父類靜態(tài)成員變量
- 執(zhí)行子類的靜態(tài)代碼塊,并初始化子類靜態(tài)成員變量
- 執(zhí)行父類的構(gòu)造代碼塊,執(zhí)行父類的構(gòu)造函數(shù),并初始化父類普通成員變量
- 執(zhí)行子類的構(gòu)造代碼塊, 執(zhí)行子類的構(gòu)造函數(shù),并初始化子類普通成員變量