Skip to content

对象初始化顺序 🚀

概述 🚀

对象初始化顺序:

  • 类加载(只执行一次):
    • 初始化父类静态变量、执行父类静态代码块
    • 初始化子类静态变量、执行子类静态代码块
  • 父类:变量、普通代码块、构造函数
  • 子类:变量、普通代码块、构造函数

总结:先父后子,依次初始化变量、执行代码块、执行构造函数。静态资源仅类加载时执行一次。

代码示例 🚀

java
package j2se;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class ObjectInitTest {
    @Test
    public void intiTest() {
        log.info(new SubClass().toString());
        log.info("-------------------");
        log.info(new SubClass("subClassField-newValue", "superClassField-newValue").toString());
    }

    @Data
    @Slf4j
    @ToString
    static
    class SuperClass {
        String superClassField = "superClassField-InitValue";
        static String superClassStaticField = "superClassStaticField-InitValue";

        static {
            log.info("SuperClass static code block running, superClassStaticField={}", superClassStaticField);
        }

        {
            log.info("SuperClass normal code block running, superClassField={}, superClassStaticField={}", superClassField, SuperClass.superClassStaticField);
        }

        public SuperClass() {
            log.info("Constructor SuperClass() is running");
        }

        public SuperClass(String superClassField) {
            this.superClassField = superClassField;
            log.info("Constructor SuperClass(String superClassField) is running");
        }
    }

    @Data
    @Slf4j
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper = true)
    static
    class SubClass extends SuperClass {
        private String subClassField = "subClassField-InitValue";
        static String subClassStaticField = "subClassStaticField-InitValue";

        static {
            log.info("SubClass static code block running, subClassStaticField={}", subClassStaticField);
        }

        {
            log.info("SubClass normal code block running, subClassField={}, subClassStaticField={}", subClassField, subClassStaticField);
        }

        public SubClass() {
            log.info("Constructor SubClass() is running");
        }

        public SubClass(String subClassField, String superClassField) {
            this.subClassField = subClassField;
            this.superClassField = superClassField;
            log.info("Constructor SubClass(String subClassField) is running");
        }
    }
}

输出 🚀

SuperClass static code block running, superClassStaticField=superClassStaticField-InitValue
SubClass static code block running, subClassStaticField=subClassStaticField-InitValue

SuperClass normal code block running, superClassField=superClassField-InitValue, superClassStaticField=superClassStaticField-InitValue
Constructor SuperClass() is running
SubClass normal code block running, subClassField=subClassField-InitValue, subClassStaticField=subClassStaticField-InitValue
Constructor SubClass() is running
ObjectInitTest.SubClass(super=ObjectInitTest.SuperClass(superClassField=superClassField-InitValue), subClassField=subClassField-InitValue)

-------------------
SuperClass normal code block running, superClassField=superClassField-InitValue, superClassStaticField=superClassStaticField-InitValue
Constructor SuperClass() is running
SubClass normal code block running, subClassField=subClassField-InitValue, subClassStaticField=subClassStaticField-InitValue
Constructor SubClass(String subClassField) is running
ObjectInitTest.SubClass(super=ObjectInitTest.SuperClass(superClassField=superClassField-newValue), subClassField=subClassField-newValue)

更新时间: