今天就跟大家聊聊有關(guān)Java中如何初始化靜態(tài)和非靜態(tài)成員變量,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)主打移動網(wǎng)站、成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)絡(luò)推廣、網(wǎng)站維護(hù)、域名與空間、等互聯(lián)網(wǎng)信息服務(wù),為各行業(yè)提供服務(wù)。在技術(shù)實(shí)力的保障下,我們?yōu)榭蛻舫兄Z穩(wěn)定,放心的服務(wù),根據(jù)網(wǎng)站的內(nèi)容與功能再決定采用什么樣的設(shè)計。最后,要實(shí)現(xiàn)符合網(wǎng)站需求的內(nèi)容、功能與設(shè)計,我們還會規(guī)劃穩(wěn)定安全的技術(shù)方案做保障。Java中非靜態(tài)成員變量、靜態(tài)成員變量的初始化時機(jī)。
非靜態(tài)變量
我們在這里分析三種結(jié)構(gòu),著重分析這三種結(jié)構(gòu)的初始化順序:
成員變量初始化語句;
成員變量初始化塊;
構(gòu)造函數(shù);
示例一:
public class MyTest { private String name = "wei.hu"; public MyTest(String name) { System.out.println("This is constructor. Will assign the variable name to: " + name + "."); System.out.println("Before the name was modified: " + this.name); this.name = name; System.out.println("After the name was modified: " + this.name); } { System.out.println("This is initialize block. Will assign the variable name to: chouchou"); System.out.println("Before the name was modified: " + this.name); this.name = "chouchou"; System.out.println("After the name was modified: " + this.name); } public String getName() { return name; } public static void main(String[] args) { MyTest myTest = new MyTest("mengna"); System.out.println(myTest.getName()); } } #輸出 This is initialize block. Will assign the variable name to: chouchou Before the name was modified: wei.hu After the name was modified: chouchou This is constructor. Will assign the variable name to: mengna. Before the name was modified: chouchou After the name was modified: mengna mengna
示例二:
public class MyTest { public MyTest(String name) { System.out.println("This is constructor. Will assign the variable name to: " + name + "."); System.out.println("Before the name was modified: " + this.name); this.name = name; System.out.println("After the name was modified: " + this.name); } private String name = "wei.hu"; { System.out.println("This is initialize block. Will assign the variable name to: chouchou"); System.out.println("Before the name was modified: " + this.name); this.name = "chouchou"; System.out.println("After the name was modified: " + this.name); } public String getName() { return name; } public static void main(String[] args) { MyTest myTest = new MyTest("mengna"); System.out.println(myTest.getName()); } } #結(jié)果(與示例一相同) This is initialize block. Will assign the variable name to: chouchou Before the name was modified: wei.hu After the name was modified: chouchou This is constructor. Will assign the variable name to: mengna. Before the name was modified: chouchou After the name was modified: mengna mengna
示例三:
public class MyTest { public MyTest(String name) { System.out.println("This is constructor. Will assign the variable name to: " + name + "."); System.out.println("Before the name was modified: " + this.name); this.name = name; System.out.println("After the name was modified: " + this.name); } { System.out.println("This is initialize block. Will assign the variable name to: chouchou"); System.out.println("Before the name was modified: " + this.name); this.name = "chouchou"; System.out.println("After the name was modified: " + this.name); } private String name = "wei.hu"; public String getName() { return name; } public static void main(String[] args) { MyTest myTest = new MyTest("mengna"); System.out.println(myTest.getName()); } } #結(jié)果 This is initialize block. Will assign the variable name to: chouchou Before the name was modified: null After the name was modified: chouchou This is constructor. Will assign the variable name to: mengna. Before the name was modified: wei.hu After the name was modified: mengna mengna 分析: 注意本示例的結(jié)果與上面兩個示例的結(jié)果不同。 1、當(dāng)我們想將成員變量name賦值為chouchou之前,發(fā)現(xiàn)this.name為null。也就是說初始化語句沒有先執(zhí)行,而是先執(zhí)行了初始化塊; 2、當(dāng)在執(zhí)行構(gòu)造函數(shù)時,我們想將成員變量name賦值為mengna,發(fā)現(xiàn)賦值之前,this.name不再是chouchou,而是wei.hu,這說明了什么? 因為初始化塊先執(zhí)行,如果緊接著執(zhí)行構(gòu)造函數(shù)的話,那么在構(gòu)造函數(shù)賦值語句執(zhí)行之前,this.name應(yīng)該是chouchou才對。但是在構(gòu)造函數(shù)賦值語句執(zhí)行之前,this.name的值變成了wei.hu,那么足以證明: 1)初始化塊先執(zhí)行; 2)下來執(zhí)行了初始化語句; 3)最后執(zhí)行了構(gòu)造函數(shù);
結(jié)論:
通過上面三個示例,我們可以發(fā)現(xiàn),對于非靜態(tài)的成員變量:
初始化語句、初始化塊,總是先于構(gòu)造函數(shù)執(zhí)行;
初始化語句、初始化塊的和執(zhí)行順序,取決于 初始化語句、初始化塊在代碼中的書寫順序。寫在上面的先執(zhí)行。
靜態(tài)變量
我們在這里也分析三種結(jié)構(gòu):
靜態(tài)初始化語句;
靜態(tài)初始化塊;
構(gòu)造函數(shù);
示例一:
public class MyTest { public static String name = "wei.hu"; public MyTest() { System.out.println("This is constructor. Will assign the variable name to: chouchou"); System.out.println("Before the name was modified: " + name); name = "chouchou"; System.out.println("After the name was modified: " + name); } static { System.out.println("This is static initialize block. Will assign the variable name to: mengna"); System.out.println("Before the name was modified: " + name); name = "mengna"; System.out.println("After the name was modified: " + name); } public static void main(String[] args) { System.out.println(MyTest.name); } } #結(jié)果 This is static initialize block. Will assign the variable name to: mengna Before the name was modified: wei.hu After the name was modified: mengna mengna 分析: 通過打印輸出,我們發(fā)現(xiàn)在執(zhí)行靜態(tài)初始快之前,靜態(tài)變量name已經(jīng)初始化為wei.hu了。也就是說: 1、靜態(tài)初始化語句先執(zhí)行; 2、下來執(zhí)行靜態(tài)初始化塊; 3、構(gòu)造函數(shù)未執(zhí)行; ---------------------
示例二:
public class MyTest { public MyTest() { System.out.println("This is constructor. Will assign the variable name to: chouchou"); System.out.println("Before the name was modified: " + MyTest.name); name = "chouchou"; System.out.println("After the name was modified: " + MyTest.name); } static { System.out.println("This is static initialize block. Will assign the variable name to: mengna"); System.out.println("Before the name was modified: " + MyTest.name); name = "mengna"; System.out.println("After the name was modified: " + MyTest.name); } public static String name = "wei.hu"; public static void main(String[] args) { System.out.println(MyTest.name); } } #結(jié)果 This is static initialize block. Will assign the variable name to: mengna Before the name was modified: null After the name was modified: mengna wei.hu 分析: 初始化塊在對靜態(tài)變量賦值之前,發(fā)現(xiàn)MyTest.name的值為空。 在最后打印出MyTest.name時,發(fā)現(xiàn)輸出的值是wei.hu,而不是mengna。也就是說,在初始化塊執(zhí)行之后,執(zhí)行了靜態(tài)初始化語句。 1、先執(zhí)行靜態(tài)初始化塊; 2、再執(zhí)行靜態(tài)初始化語句; 3、構(gòu)造函數(shù)未執(zhí)行; ---------------------
結(jié)論:
對于靜態(tài)字段,初始化有如下規(guī)則:
1. 若靜態(tài)初始化語句在前,靜態(tài)代碼塊在后,則先執(zhí)行靜態(tài)初始化語句;
2. 若靜態(tài)代碼塊在前,靜態(tài)初始化語句在后,則先執(zhí)行靜態(tài)代碼塊;
看完上述內(nèi)容,你們對Java中如何初始化靜態(tài)和非靜態(tài)成員變量有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝大家的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。