從今天開始進(jìn)行基本功的提升,(本人很菜,寫的可能不好,歡迎大家提出意見,此外,引用別人的東西,我會全部標(biāo)識出),我不知道,這個過程會什么時候結(jié)束,總之,我會努力的把這個過程記錄下來。不想提升技術(shù)的程序員不是好程序員。
Java 靜態(tài)代碼塊,構(gòu)造代碼塊,構(gòu)造函數(shù)
/**
* @ClassName: TestA
* @Description: TODO
* @Author: kevin
* @Date: 2019-03-20 19:24
* @Version: 1.0
**/
public class TestA {
static int value = 1;
static {
System.out.println("This is TestA 靜態(tài)代碼塊");
value++;
System.out.println(value);
}
{
System.out.println("This is TestA 構(gòu)造代碼塊");
}
public TestA() {
System.out.println("This is TestA 無參構(gòu)造函數(shù)");
}
public TestA(String str) {
System.out.println("This is TestA 有參構(gòu)造函數(shù): str = " + str);
}
public static void main(String[] args) {
System.out.println("===============");
TestA a = new TestA();
System.out.println("---------------");
TestA b = new TestA("B");
}
static {
value *= 2;
System.out.println(value);
}
}
這里通過一段簡單的代碼,引入今天的內(nèi)容。首先簡單了解標(biāo)題中的各個概念
靜態(tài)代碼塊
- 用static聲明,每個靜態(tài)代碼塊只會執(zhí)行一次,在JVM加載類的時候會執(zhí)行靜態(tài)代碼塊,注意優(yōu)先于主函數(shù)。
- 靜態(tài)代碼塊負(fù)責(zé)初始化類,因此不能存在于任何方法體內(nèi),不存在被對象調(diào)用。
- 靜態(tài)代碼塊可以有多個,其執(zhí)行順序是,先定義,先執(zhí)行。
構(gòu)造代碼塊
- 用{}聲明,構(gòu)造代碼塊的作用是給對象進(jìn)行初始化。
- 和靜態(tài)代碼塊不同,構(gòu)造代碼塊在創(chuàng)對象時被調(diào)用,因此,調(diào)用次數(shù)可能不止一次,也僅僅只能被對象調(diào)用,且執(zhí)行順序優(yōu)先于構(gòu)造函數(shù)。
- 構(gòu)造代碼塊的作用是給所有對象進(jìn)行統(tǒng)一的初始化,因?yàn)閮?yōu)先級高于構(gòu)造函數(shù)。
構(gòu)造函數(shù)
- 創(chuàng)建對象時,就會調(diào)用對應(yīng)的構(gòu)造函數(shù),注意實(shí)際中構(gòu)造函數(shù)可能不止一個,因此調(diào)用不同的構(gòu)造函數(shù),就會初始化不同實(shí)例對象。
- 對象創(chuàng)建時,構(gòu)造函數(shù)只能調(diào)用一次。
- 同構(gòu)造代碼塊一樣,只能被對象調(diào)用,不能被類調(diào)用。
通過概念的介紹就會清晰地知道上述代碼的運(yùn)行結(jié)果
This is TestA 靜態(tài)代碼塊
2
4
===============
This is TestA 構(gòu)造代碼塊
This is TestA 無參構(gòu)造函數(shù)
---------------
This is TestA 構(gòu)造代碼塊
This is TestA 有參構(gòu)造函數(shù): str = B
靜態(tài)變量,靜態(tài)代碼塊,變量,構(gòu)造代碼塊,構(gòu)造函數(shù),執(zhí)行的順序
靜態(tài)變量,靜態(tài)代碼塊 > 變量,構(gòu)造代碼塊 > 構(gòu)造函數(shù)
/**
* @ClassName: TestB
* @Description: TODO
* @Author: kevin
* @Date: 2019-03-20 20:06
* @Version: 1.0
**/
public class TestB {
public static String staticZone = "這是靜態(tài)變量";
public String variable;
public String variable2 = "haha";
static {
System.out.println("這是靜態(tài)代碼塊");
System.out.println(staticZone);
}
{
System.out.println("這是構(gòu)造代碼塊");
System.out.println(variable);
System.out.println(variable2);
System.out.println(staticZone);
}
public TestB() {
System.out.println("這是無參構(gòu)造函數(shù)");
}
public TestB(String str) {
System.out.println("This is TestB 有參構(gòu)造函數(shù): str = " + str);
}
public static void main(String[] args) {
System.out.println("========");
TestB a = new TestB();
System.out.println("--------");
TestB b = new TestB("B");
}
}
運(yùn)行結(jié)果
這是靜態(tài)代碼塊
這是靜態(tài)變量
========
這是構(gòu)造代碼塊
null
haha
這是靜態(tài)變量
這是無參構(gòu)造函數(shù)
--------
這是構(gòu)造代碼塊
null
haha
這是靜態(tài)變量
This is TestB 有參構(gòu)造函數(shù): str = B
繼承時,執(zhí)行順序
- 執(zhí)行父類靜態(tài)代碼塊,并初始化父類的靜態(tài)成員變量
- 執(zhí)行子類靜態(tài)代碼塊,并初始化子類的靜態(tài)成員變量
- 依次執(zhí)行父類的構(gòu)造代碼塊,構(gòu)造函數(shù),并初始化父類普通的成員變量
- 依次執(zhí)行子類的構(gòu)造代碼塊,構(gòu)造函數(shù),并初始化子類普通的成員變量
/**
* @ClassName: A
* @Description: TODO
* @Author: kevin
* @Date: 2019-03-20 20:28
* @Version: 1.0
**/
public class A {
static {
System.out.println("This is A 靜態(tài)代碼塊");
}
{
System.out.println("This is A 構(gòu)造代碼塊");
}
public A() {
System.out.println("This is A 無參構(gòu)造函數(shù)");
}
public A(String str) {
System.out.println("This is A 有參構(gòu)造函數(shù): str = " + str);
}
}
/**
* @ClassName: B
* @Description: TODO
* @Author: kevin
* @Date: 2019-03-20 20:30
* @Version: 1.0
**/
public class B extends A {
static {
System.out.println("This is B 靜態(tài)代碼塊");
}
{
System.out.println("This is B 構(gòu)造代碼塊");
}
public B() {
System.out.println("This is B 無參構(gòu)造函數(shù)");
}
public B(String str) {
System.out.println("This is B 有參構(gòu)造函數(shù): str = " + str);
}
public B(String str, int tmp) {
super(str);
System.out.println("This is B 有參構(gòu)造函數(shù): str = " + str + " tmp = " + tmp);
}
public static void main(String[] args) {
System.out.println("===============");
B a = new B();
System.out.println("---------------");
B b = new B("B");
System.out.println("---------------");
B C = new B("C", 6);
}
}
執(zhí)行結(jié)果,注意實(shí)例對象c的執(zhí)行過程有別于實(shí)例對象b。
This is A 靜態(tài)代碼塊
This is B 靜態(tài)代碼塊
===============
This is A 構(gòu)造代碼塊
This is A 無參構(gòu)造函數(shù)
This is B 構(gòu)造代碼塊
This is B 無參構(gòu)造函數(shù)
---------------
This is A 構(gòu)造代碼塊
This is A 無參構(gòu)造函數(shù)
This is B 構(gòu)造代碼塊
This is B 有參構(gòu)造函數(shù): str = B
---------------
This is A 構(gòu)造代碼塊
This is A 有參構(gòu)造函數(shù): str = C
This is B 構(gòu)造代碼塊
This is B 有參構(gòu)造函數(shù): str = C tmp = 6
參考文章鏈接
Java提高篇——靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造函數(shù)以及Java類初始化順序
Java普通代碼塊,構(gòu)造代碼塊,靜態(tài)代碼塊區(qū)別,執(zhí)行順序的代碼實(shí)例