Java靜態(tài)和非靜態(tài)成員變量初始化過(guò)程解析
這篇文章主要介紹了Java靜態(tài)和非靜態(tài)成員變量初始化過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Java中非靜態(tài)成員變量、靜態(tài)成員變量的初始化時(shí)機(jī)。
非靜態(tài)變量
我們?cè)谶@里分析三種結(jié)構(gòu),著重分析這三種結(jié)構(gòu)的初始化順序:
- 成員變量初始化語(yǔ)句;
 - 成員變量初始化塊;
 - 構(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é)果與上面兩個(gè)示例的結(jié)果不同。
1、當(dāng)我們想將成員變量name賦值為chouchou之前,發(fā)現(xiàn)this.name為null。也就是說(shuō)初始化語(yǔ)句沒(méi)有先執(zhí)行,而是先執(zhí)行了初始化塊;
2、當(dāng)在執(zhí)行構(gòu)造函數(shù)時(shí),我們想將成員變量name賦值為mengna,發(fā)現(xiàn)賦值之前,this.name不再是chouchou,而是wei.hu,這說(shuō)明了什么?
  因?yàn)槌跏蓟瘔K先執(zhí)行,如果緊接著執(zhí)行構(gòu)造函數(shù)的話,那么在構(gòu)造函數(shù)賦值語(yǔ)句執(zhí)行之前,this.name應(yīng)該是chouchou才對(duì)。但是在構(gòu)造函數(shù)賦值語(yǔ)句執(zhí)行之前,this.name的值變成了wei.hu,那么足以證明:
  1)初始化塊先執(zhí)行;
  2)下來(lái)執(zhí)行了初始化語(yǔ)句;
  3)最后執(zhí)行了構(gòu)造函數(shù);
結(jié)論: 
通過(guò)上面三個(gè)示例,我們可以發(fā)現(xiàn),對(duì)于非靜態(tài)的成員變量:
初始化語(yǔ)句、初始化塊,總是先于構(gòu)造函數(shù)執(zhí)行;
初始化語(yǔ)句、初始化塊的和執(zhí)行順序,取決于 初始化語(yǔ)句、初始化塊在代碼中的書(shū)寫(xiě)順序。寫(xiě)在上面的先執(zhí)行。
靜態(tài)變量
我們?cè)谶@里也分析三種結(jié)構(gòu):
- 靜態(tài)初始化語(yǔ)句;
 - 靜態(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
分析:
通過(guò)打印輸出,我們發(fā)現(xiàn)在執(zhí)行靜態(tài)初始快之前,靜態(tài)變量name已經(jīng)初始化為wei.hu了。也就是說(shuō):
1、靜態(tài)初始化語(yǔ)句先執(zhí)行;
2、下來(lái)執(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
分析:
初始化塊在對(duì)靜態(tài)變量賦值之前,發(fā)現(xiàn)MyTest.name的值為空。 在最后打印出MyTest.name時(shí),發(fā)現(xiàn)輸出的值是wei.hu,而不是mengna。也就是說(shuō),在初始化塊執(zhí)行之后,執(zhí)行了靜態(tài)初始化語(yǔ)句。
1、先執(zhí)行靜態(tài)初始化塊;
2、再執(zhí)行靜態(tài)初始化語(yǔ)句;
3、構(gòu)造函數(shù)未執(zhí)行;
---------------------
結(jié)論: 
對(duì)于靜態(tài)字段,初始化有如下規(guī)則: 
1. 若靜態(tài)初始化語(yǔ)句在前,靜態(tài)代碼塊在后,則先執(zhí)行靜態(tài)初始化語(yǔ)句; 
2. 若靜態(tài)代碼塊在前,靜態(tài)初始化語(yǔ)句在后,則先執(zhí)行靜態(tài)代碼塊;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:spring AOP的After增強(qiáng)實(shí)現(xiàn)方法實(shí)例分析
欄 目:Java
下一篇:詳解springboot中使用異步的常用兩種方式及其比較
本文標(biāo)題:Java靜態(tài)和非靜態(tài)成員變量初始化過(guò)程解析
本文地址:http://www.jygsgssxh.com/a1/Java/8707.html
您可能感興趣的文章
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
 - 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
 - 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
 - 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
 - 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
 - 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
 - 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
 - 01-10淺談Java中真的只有值傳遞么
 - 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
 - 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問(wèn)題
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
 - 01-10Springboot中@Value的使用詳解
 - 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
 - 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
 - 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
 - 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
 - 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
 - 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
 - 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
 - 01-10淺談Java中真的只有值傳遞么
 
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
 - 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 


