欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

這篇文章主要介紹了Java靜態(tài)和非靜態(tài)成員變量初始化過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

Java中非靜態(tài)成員變量、靜態(tài)成員變量的初始化時機。

非靜態(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring boot 實現(xiàn)配置多個DispatcherServlet最簡單方式

    spring boot 實現(xiàn)配置多個DispatcherServlet最簡單方式

    這篇文章主要介紹了spring boot 實現(xiàn)配置多個DispatcherServlet最簡單方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • java分布式事務(wù)之可靠消息最終一致性解決方案

    java分布式事務(wù)之可靠消息最終一致性解決方案

    這篇文章主要為大家介紹了java分布式事務(wù)之可靠消息最終一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Java中ArrayList的工作原理詳解

    Java中ArrayList的工作原理詳解

    本文主要介紹了Java中ArrayList的工作原理,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • SpringBoot集成Auth0 JWT的示例代碼

    SpringBoot集成Auth0 JWT的示例代碼

    本文主要介紹了SpringBoot集成Auth0 JWT的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • java多線程實現(xiàn)下載圖片并壓縮

    java多線程實現(xiàn)下載圖片并壓縮

    這篇文章主要為大家詳細介紹了java多線程實現(xiàn)下載圖片并壓縮,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 圖文詳解Java環(huán)境變量配置方法

    圖文詳解Java環(huán)境變量配置方法

    這篇文章主要以圖文結(jié)合的方式詳細介紹了Java環(huán)境變量配置方法,文中步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • SpringMVC中的@RequestMapping注解的使用詳細教程

    SpringMVC中的@RequestMapping注解的使用詳細教程

    @RequestMapping注解的作用就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系,本文主要來和大家詳細講講它的具體使用,感興趣的可以了解一下
    2023-07-07
  • Java分別利用深度優(yōu)先和廣度優(yōu)先求解迷宮路徑

    Java分別利用深度優(yōu)先和廣度優(yōu)先求解迷宮路徑

    這篇文章主要為大家詳細介紹了Java如何利用深度優(yōu)先的非遞歸遍歷方法和廣度優(yōu)先的遍歷方法實現(xiàn)求解迷宮路徑,文中的示例代碼講解詳細,需要的可以參考一下
    2022-08-08
  • idea.vmoptions 最佳配置方案

    idea.vmoptions 最佳配置方案

    本文介紹了針對IntelliJ IDEA的優(yōu)化配置建議,包括提升內(nèi)存設(shè)置、啟用G1垃圾回收器、優(yōu)化垃圾回收策略以及調(diào)整網(wǎng)絡(luò)設(shè)置等,旨在提高IDE的性能和響應(yīng)速度,同時,指導(dǎo)用戶如何修改vmoptions文件以應(yīng)用這些配置,并提供了監(jiān)控內(nèi)存使用和插件管理的建議
    2024-09-09
  • Java流處理stream使用詳解

    Java流處理stream使用詳解

    Java8的另一大亮點Stream,它與java.io包里的InputStream和OutputStream是完全不同的概念,下面這篇文章主要給大家介紹了關(guān)于Java8中Stream詳細使用方法的相關(guān)資料,需要的朋友可以參考下
    2022-10-10

最新評論