Java execution sequence static code block, code block non-static, static methods and constructors

1 of static code block: some code must be executed when the project started, the execution of such code is active (when the class is loaded, static code block is executed, and only be executed once, static blocks used to perform class attributes initialization)

2 static methods: you need to be initialized when the project started, without creating an object, such code is executed passive (static methods in the class loading time has been loaded with the class name can be called directly).

The difference is: static block of code is automatically executed,

Static methods are invoked only when they were executed.

Static block of code, the virtual machine when loading classes will be loaded for execution and performed only once;

Non-static block of code (that is, a new object when) executed when creating an object, the object will be executed every time you create a

The method does not create a static class execution and does not result in a static or non-static block code block.

 

The method does not create a static class execution and does not result in a static code block or a static block of code, for example:

package com.practice.dynamicproxy;

class Parent {
    static String name = "hello";
    {
        System.out.println("parent block");
    }
    static {
        System.out.println("parent static block");
    }

    public Parent() {
        System.out.println("parent constructor");
    }
    
}

class Child extends Parent {
    static String childName = "hello";
    {
        System.out.println("child block");
    }
    static {
        System.out.println("child static block");
    }

    public Child() {
        System.out.println("child constructor");
    }
}

public class StaticIniBlockOrderTest {
    public static void test1() {
        System.out.println("static method");
    }
    public static void main(String[] args) {
//        new Child();// 语句(*)
//        new Child();
        StaticIniBlockOrderTest.test1();
    }
}

Implementation of the results are as follows:

static method

 

The rest of the sample code is as follows:

package com.practice.dynamicproxy;

class Parent {
    static String name = "hello";
    {
        System.out.println("parent block");
    }
    static {
        System.out.println("parent static block");
    }

    public Parent() {
        System.out.println("parent constructor");
    }
    
}

class Child extends Parent {
    static String childName = "hello";
    {
        System.out.println("child block");
    }
    static {
        System.out.println("child static block");
    }

    public Child() {
        System.out.println("child constructor");
    }
}

public class StaticIniBlockOrderTest {
    public static void test1() {
        System.out.println("static method");
    }
    public static void main(String[] args) {
        new Child();// 语句(*)
        new Child();
//        StaticIniBlockOrderTest.test1();
    }
}

Implementation of the results are as follows:

parent static block
child static block
parent block
parent constructor
child block
child constructor
parent block
parent constructor
child block
child constructor

Description: static block of code will execute once, twice non-static block of code, the first code block and perform non-static constructors parent class, then the implementation of the class code blocks and non-static constructors

Guess you like

Origin www.cnblogs.com/linwenbin/p/11084830.html