Java static code block, construction code block, construction method execution order

Java static code block, construction code block, construction method execution order

Static code takes precedence over non-static code because the members modified by static are class members and will be executed when the JVM loads the class, and members not modified by static are also called instance members, and objects need to be created will then be loaded into heap memory. So the static will take precedence over the non-static. When executing the constructor (constructor method), there are three implicit steps
before executing the method body 1, super statement , the following three situations may occur:  1) The first line of the constructor body is this statement, then it will not be executed Implicit three steps,  2) The first line of the constructor body is a super statement, then the corresponding parent class constructor is called,  3) The first line of the constructor body is neither this statement nor a super statement, then the implicit call super(), the default constructor of its parent class, which is why a parent class usually provides a default constructor;  2. Initialize non-static variables3. Construct code blocksIt can be seen that the construction code block takes precedence over the method body of the construction method , but the this keyword and the super keyword cannot appear at the same time, and can only be in the first line of the code. If the this keyword is present, the implicit three-step will not be executed. For example, analyzing the following code and execution results, the execution steps Step 1–Step 7 have been marked with comments. That is to say, when multiple constructors are called recursively, the construction code block will only be executed before the last constructor (that is, the first line of the method body is not the this statement) is executed!








public class Test {
    public static int a = 0;

    static {// Step 1
        a = 10;
        System.out.println("静态代码块在执行a=" + a);
    }

    {// Step 4
        a = 8;
        System.out.println("非静态代码块(构造代码块)在执行a=" + a);
    }

    public Test() {
        this("调用带参构造方法1,a=" + a); // Step 2
        System.out.println("无参构造方法在执行a=" + a);// Step 7
    }

    public Test(String n) {
        this(n, "调用带参构造方法2,a=" + a); // Step 3
        System.out.println("带参构造方法1在执行a=" + a); // Step 6
    }

    public Test(String s1, String s2) {
        System.out.println(s1 + ";" + s2);// Step 5
    }

    public static void main(String[] args) {
        Test t = null;// JVM加载Test类,静态代码块执行
        System.out.println("下面new一个Test实例:");
        t = new Test();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

Results of the:

静态代码块在执行a=10
下面new一个Test实例:
非静态代码块(构造代码块)在执行a=8
调用带参构造方法1a=10;调用带参构造方法2a=10
带参构造方法1在执行a=8
无参构造方法在执行a=8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

http://bbs.csdn.net/topics/391001417

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325806846&siteId=291194637