Java类成员的初始化顺序

前两天去面试,面试官问类成员的初始化顺序,比如静态块、构造函数、成员变量这些,然后再有父类、子类... 如此这般,我是脑子里快浆糊了,看来基本功不扎实啊,所以我决定仔细研究一下,再写出来与大家分享一下。

1、普通成员变量与构造函数

public class Example {
    int number = 1;

    public Example() {
        number = 2;
    }
}
 

执行:

Example example = new Example();

Log.d("TAG", "number = " + example.number);

执行结果:

number = 2

结果为2,表明构造函数是在之后才执行的

顺序从高到低为 :1、普通成员变量  2、构造函数

2、普通成员变量、构造函数与静态成员变量

public class Base {
    public Base(String s) {
        Log.d("TAG", s);
    }
}
public class Example {
    Base base1 = new Base("common member variable");
    static Base base2 = new Base("static member variable");


    public Example() {
        Log.d("TAG", " Example constructor");
    }
}

执行:

Example example = new Example();

执行结果:
static member variable
common member variable
Example constructor
顺序从高到低为 :1、静态成员变量  2、 普通成员变量  3、构造函数

3、普通成员变量、构造函数、静态成员变量与静态初始化块

public class Example {
    Base base1 = new Base("common member variable");
    static Base base2 = new Base("static member variable");

    public Example() {
        Log.d("TAG", "Example constructor");
    }

    static {
        Log.d("TAG", "static module");
    }

执行:

Example example = new Example();

执行结果:
static member variable
static module
common member variable
Example constructor

顺序从高到低为 :1、静态成员变量  2、静态初始化模块 3、 普通成员变量  4、构造函数,是这样的吗?我们把定义静态变量的顺序换一下,看看结果
public class Example {
    Base base1 = new Base("common member variable");

    public Example() {
        Log.d("TAG", "Example constructor");
    }

    static {
        Log.d("TAG", "static module");
    }

    static Base base2 = new Base("static member variable");
}
执行结果:
static module
static member variable
common member variable
Example constructor

可以看出来,静态变量与静态模块的初始化顺序与它们的位置有关
所以,顺序从高到低为 :1、静态成员变量或 静态初始化模块 2、 普通成员变量  3、构造函数

4、普通成员变量、构造函数、静态成员变量、静态初始化块与非静态模块

public class Example {
    Base base1 = new Base("common member variable");

    public Example() {
        Log.d("TAG", "Example constructor");
    }

    {
        Log.d("TAG", "common module");
    }

    static {
        Log.d("TAG", "static module");
    }
    static Base base2 = new Base("static member variable");
}
执行结果:
static module
static member variable
common member variable
common module
Example constructor
再看看非静态模块是不是跟上面的代码有类似呢?
public class Example {

    public Example() {
        Log.d("TAG", "Example constructor");
    }

    {
        Log.d("TAG", "common module");
    }

    Base base1 = new Base("common member variable");

    static {
        Log.d("TAG", "static module");
    }
    static Base base2 = new Base("static member variable");
}
执行结果:
static module
static member variable
common module
common member variable
Example constructor
看来果然是这样的。
顺序从高到低为 :1、静态成员变量或 静态初始化模块 2、 普通成员变量或非静态模块  3、构造函数

5、继承关系中的初始化情况

public class Parent {

    public Parent() {
        Log.d("TAG", "Parent constructor");
    }

    {
        Log.d("TAG", "Parent common module");
    }

    Base base1 = new Base("Parent common member variable");

    static {
        Log.d("TAG", "Parent static module");
    }
    static Base base2 = new Base("Parent static member variable");
}

public class Child extends Parent {

    public Child() {
        Log.d("TAG", "Child constructor");
    }

    {
        Log.d("TAG", "Child common module");
    }

    Base base1 = new Base("Child common member variable");

    static {
        Log.d("TAG", "Child static module");
    }
    static Base base2 = new Base("Child static member variable");
}
执行
Child child = new Child();
执行结果为:
Parent static module
Parent static member variable
Child static module
Child static member variable
Parent common module
Parent common member variable
Parent constructor
Child common module
Child common member variable
Child constructor


今天就是这些了,如果有不对的地方,欢迎大家纠正~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值