java基礎(chǔ)之類初始化順序示例解析
初始化順序
在類中變量定義的順序決定了它們初始化的順序。在創(chuàng)建任何java對(duì)象時(shí),都是依次調(diào)用父類非靜態(tài)初始化塊、父類構(gòu)造器執(zhí)行初始化、本類的非靜態(tài)初始化塊、本類構(gòu)造器執(zhí)行初始化
public class House {
// 構(gòu)造器之前
Window w1 = new Window(1);
House(){
System.out.println("House()");
Window window = new Window(11);
}
// 構(gòu)造器之后
Window w2 = new Window(2);
void f(){
System.out.println("f()");
}
Window w3 = new Window(3);
public static void main(String[] args) {
House house = new House();
house.f();
}
}
public class Window {
public Window(int mark){
System.out.println("Window("+mark+")");
}
}執(zhí)行結(jié)果
Window(1)
Window(2)
Window(3)
House()
Window(11)
f()
由執(zhí)行結(jié)果可知,在進(jìn)行對(duì)象實(shí)例化時(shí)先執(zhí)行初始化塊,再執(zhí)行構(gòu)造器主體部分
驗(yàn)證類加載
public class Initable {
// 非編譯期常量
public static final int COUNT =new Random().nextInt(1000);
static {
System.out.println("Initable初始化");
}
}
public class Initable1 {
// 編譯期常量
public static final int COUNT = 47;
static {
System.out.println("Initable1初始化");
}
}
public class Initable2 {
public static int COUNT = 56;
static {
System.out.println("Initable2初始化");
}
}
public class Initable3 {
public static int COUNT = 33;
static {
System.out.println("Initable3初始化");
}
}
public class Initable4 {
public static int COUNT = 44;
static {
System.out.println("Initable4初始化");
}
}
public class Test {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
System.out.println("Initable---------------");
System.out.println(Initable.COUNT);
System.out.println("Initable1---------------");
System.out.println(Initable1.COUNT);
System.out.println("Initable2---------------");
System.out.println(Initable2.COUNT);
System.out.println("Initable3---------------");
Class<Initable3> clazz = Initable3.class;
System.out.println("Initable4---------------");
Class.forName("com.zhanghe.study.init.Initable4");
}
}結(jié)果:
Initable---------------
Initable初始化
457
Initable1---------------
47
Initable2---------------
Initable2初始化
56
Initable3---------------
Initable4---------------
Initable4初始化
Initable和Initable1結(jié)果分析
對(duì)于static final的值是一個(gè)編譯期常量的話(如Initable1.COUNT),獲取這個(gè)值時(shí)不需要對(duì)Initable1進(jìn)行初始化就可以讀取,如果用static final的值不是一個(gè)編譯期常量(如Initable.COUNT),訪問這個(gè)變量會(huì)強(qiáng)制對(duì)該類進(jìn)行初始化
Initable2結(jié)果分析
對(duì)于一個(gè)僅僅是static修飾的字段而不是final的,在讀取這個(gè)字段之前,需要為該字段分配存儲(chǔ)空間以及初始化該存儲(chǔ)空間
Initable3和Initable4結(jié)果分析
使用.class語法不會(huì)對(duì)類進(jìn)行初始化,而使用Class.forName()來產(chǎn)生Class引用會(huì)直接引發(fā)類的初始化
注意:如果沒有顯式的編寫構(gòu)造器的話,java編譯器會(huì)默認(rèn)提供一個(gè)無參構(gòu)造器,但是如果提供了自己的構(gòu)造器,編譯器將不會(huì)再生成默認(rèn)構(gòu)造器。
java基礎(chǔ)/面向?qū)ο?4.java基礎(chǔ)之初始化/
以上就是java基礎(chǔ)之類初始化順序示例解析的詳細(xì)內(nèi)容,更多關(guān)于java類初始化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java?Swing實(shí)現(xiàn)自定義窗口CusFrame
文章介紹了自定義Swing窗口CusFrame的核心功能,包括移除系統(tǒng)默認(rèn)標(biāo)題欄、實(shí)現(xiàn)圓角矩形形狀、支持拖拽移動(dòng)和調(diào)整大小、多屏幕適配以及窗口狀態(tài)管理等功能,最后文章提供了使用示例和注意事項(xiàng),有需要的小伙伴可以參考下2026-05-05
Java 其中翻轉(zhuǎn)字符串的實(shí)現(xiàn)方法
這篇文章主要介紹了Java 其中翻轉(zhuǎn)字符串的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
很多人竟然不知道Java線程池的創(chuàng)建方式有7種
本文主要介紹了很多人竟然不知道Java線程池的創(chuàng)建方式有7種,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Spring實(shí)戰(zhàn)之獲得Bean本身的id操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之獲得Bean本身的id操作,結(jié)合實(shí)例形式分析了spring獲取Bean本身id的相關(guān)配置與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-11-11
java 實(shí)現(xiàn)數(shù)組擴(kuò)容與縮容案例
這篇文章主要介紹了java 實(shí)現(xiàn)數(shù)組擴(kuò)容與縮容案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02

