最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java靜態(tài)和非靜態(tài)成員變量初始化過(guò)程解析

 更新時(shí)間:2020年01月09日 15:14:36   作者:戈博折刀  
這篇文章主要介紹了Java靜態(tài)和非靜態(tài)成員變量初始化過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了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ǔ)句、初始化塊在代碼中的書寫順序。寫在上面的先執(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中JWT(JSON?Web?Token)的運(yùn)用具體案例

    Java中JWT(JSON?Web?Token)的運(yùn)用具體案例

    這篇文章主要介紹了Java中JWT(JSON?Web?Token)的運(yùn)用具體案例,JWT(JSON?Web?Token)是一種開放標(biāo)準(zhǔn),用于在網(wǎng)絡(luò)應(yīng)用環(huán)境中安全地傳遞信息,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-11-11
  • IntelliJ IDEA 2021.1 EAP 4 發(fā)布:字體粗細(xì)可調(diào)整Git commit template 支持

    IntelliJ IDEA 2021.1 EAP 4 發(fā)布:字體粗細(xì)可調(diào)整Git commit template 支持

    這篇文章主要介紹了IntelliJ IDEA 2021.1 EAP 4 發(fā)布:字體粗細(xì)可調(diào)整,Git commit template 支持,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Java查找不重復(fù)無(wú)序數(shù)組中是否存在兩個(gè)數(shù)字的和為某個(gè)值

    Java查找不重復(fù)無(wú)序數(shù)組中是否存在兩個(gè)數(shù)字的和為某個(gè)值

    今天小編就為大家分享一篇關(guān)于Java查找不重復(fù)無(wú)序數(shù)組中是否存在兩個(gè)數(shù)字的和為某個(gè)值,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • java實(shí)現(xiàn)簡(jiǎn)單發(fā)送郵件功能

    java實(shí)現(xiàn)簡(jiǎn)單發(fā)送郵件功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單發(fā)送郵件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 淺談spring中的default-lazy-init參數(shù)和lazy-init

    淺談spring中的default-lazy-init參數(shù)和lazy-init

    下面小編就為大家?guī)?lái)一篇淺談spring中的default-lazy-init參數(shù)和lazy-init。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 詳解Java異常處理最佳實(shí)踐及陷阱防范

    詳解Java異常處理最佳實(shí)踐及陷阱防范

    這篇文章主要介紹了Java異常處理最佳實(shí)踐及陷阱防范,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java聊天室之實(shí)現(xiàn)聊天室客戶端功能

    Java聊天室之實(shí)現(xiàn)聊天室客戶端功能

    這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易聊天室之實(shí)現(xiàn)聊天室客戶端功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下
    2022-11-11
  • springboot實(shí)現(xiàn)將自定義日志格式存儲(chǔ)到mongodb中

    springboot實(shí)現(xiàn)將自定義日志格式存儲(chǔ)到mongodb中

    這篇文章主要介紹了springboot實(shí)現(xiàn)將自定義日志格式存儲(chǔ)到mongodb中的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 基于SpringBoot和Vue3的博客平臺(tái)的用戶注冊(cè)與登錄功能實(shí)現(xiàn)

    基于SpringBoot和Vue3的博客平臺(tái)的用戶注冊(cè)與登錄功能實(shí)現(xiàn)

    本教程將指導(dǎo)您如何使用Spring?Boot和Vue3實(shí)現(xiàn)用戶注冊(cè)與登錄功能。我們將使用Spring?Boot作為后端框架,Vue3作為前端框架,同時(shí)使用MySQL作為數(shù)據(jù)庫(kù),感興趣的朋友可以參考一下
    2023-04-04
  • SpringBoot實(shí)現(xiàn)HTTP服務(wù)監(jiān)聽的代碼示例

    SpringBoot實(shí)現(xiàn)HTTP服務(wù)監(jiān)聽的代碼示例

    前后端分離項(xiàng)目中,在調(diào)用接口調(diào)試時(shí)候,我們可以通過(guò)cpolar內(nèi)網(wǎng)穿透將本地服務(wù)端接口模擬公共網(wǎng)絡(luò)環(huán)境遠(yuǎn)程調(diào)用調(diào)試,本次教程我們以Java服務(wù)端接口為例,需要的朋友可以參考下
    2023-05-05

最新評(píng)論

会同县| 天峻县| 富民县| 阿勒泰市| 五常市| 屏南县| 武陟县| 青田县| 昌黎县| 湖北省| 稻城县| 崇礼县| 绵阳市| 青州市| 巴林左旗| 建宁县| 永川市| 体育| 南郑县| 鄂托克旗| 临武县| 灵台县| 铁岭市| 浙江省| 金山区| 长泰县| 通河县| 朔州市| 建昌县| 绥德县| 罗平县| 保康县| 额尔古纳市| 甘肃省| 济宁市| 衡东县| 商丘市| 长子县| 柘城县| 丰宁| 安顺市|