Analysis of Java static and non static member variable initialization process

This article mainly introduces the analysis of Java static and non static member variable initialization process. It is introduced in great detail through the example code, which has a certain reference value for everyone's study or work. Friends in need can refer to it

Initialization timing of non static member variables and static member variables in Java.

Non static variable

Here we analyze three structures, focusing on the initialization order of the three structures:

Example 1:

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

Example 2:

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());
  }
}

#结果(与示例一相同)
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

Example 3:

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());
  }
}

#结果
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

分析:
注意本示例的结果与上面两个示例的结果不同。
1、当我们想将成员变量name赋值为chouchou之前,发现this.name为null。也就是说初始化语句没有先执行,而是先执行了初始化块;
2、当在执行构造函数时,我们想将成员变量name赋值为mengna,发现赋值之前,this.name不再是chouchou,而是wei.hu,这说明了什么?
  因为初始化块先执行,如果紧接着执行构造函数的话,那么在构造函数赋值语句执行之前,this.name应该是chouchou才对。但是在构造函数赋值语句执行之前,this.name的值变成了wei.hu,那么足以证明:
  1)初始化块先执行;
  2)下来执行了初始化语句;
  3)最后执行了构造函数;

Conclusion:

Through the above three examples, we can find that for non static member variables:

Initialization statements and initialization blocks are always executed before constructors;

The and execution order of initialization statements and initialization blocks depend on the writing order of initialization statements and initialization blocks in the code. What is written above shall be executed first.

Static variable

We also analyze three structures here:

Example 1:

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);
  }
}

#结果
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

分析:
通过打印输出,我们发现在执行静态初始快之前,静态变量name已经初始化为wei.hu了。也就是说:
1、静态初始化语句先执行;
2、下来执行静态初始化块;
3、构造函数未执行;
---------------------

Example 2:

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);
  }
}

#结果
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

分析:
初始化块在对静态变量赋值之前,发现MyTest.name的值为空。 在最后打印出MyTest.name时,发现输出的值是wei.hu,而不是mengna。也就是说,在初始化块执行之后,执行了静态初始化语句。
1、先执行静态初始化块;
2、再执行静态初始化语句;
3、构造函数未执行;
---------------------

Conclusion:

For static fields, the initialization rules are as follows:

1. If the static initialization statement comes first and the static code block comes second, execute the static initialization statement first;

2. If the static code block is before the static initialization statement, execute the static code block first;

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>