java类中成员的初始化顺序,Java类成员初始化顺序

Java中的类属性初始化顺序规则是:

1、静态代码块或者属性->普通代码块或者属性->构造函数

再加上两条补充规则:

2、父类先与子类初始化

3、同是静态或者非静态的方法和属性的初始化顺序是按照它们定义的顺序来的

通过代码验证如下:

public class Attr {

public Attr(){

System.out.println("attr construct.");

}

public Attr(String name){

System.out.println(name + " attr construct.");

}

}

public class Super {

{

System.out.println("super normal block_1");

}

static {

System.out.println("super static block_1.");

}

private Attr normalAttr1 = new Attr("superNormalAttr_1");

public Super(){

System.out.println("super construct.");

}

private static Attr staticAttr1 = new Attr("superStaticAttr_1");

static {

System.out.println("super static block_2.");

}

{

System.out.println("super normal block_2.");

}

}

public class Extend extends Super {

private Attr normalAttr1 = new Attr("extendNormalAttr_1");

{

System.out.println("extend normal block_1");

}

static {

System.out.println("extend static block_1.");

}

public Extend(){

System.out.println("extend construct.");

}

static {

System.out.println("extend static block_2.");

}

{

System.out.println("extend normal block_2.");

}

private static Attr staticAttr1 = new Attr("extendStaticAttr_1");

public static void main(String[] args) {

Extend extend = new Extend();

}

}

测试结果如下:

super static block_1.

superStaticAttr_1 attr construct.

super static block_2.

extend static block_1.

extend static block_2.

extendStaticAttr_1 attr construct.

super normal block_1

superNormalAttr_1 attr construct.

super normal block_2.

super construct.

extendNormalAttr_1 attr construct.

extend normal block_1

extend normal block_2.

extend construct.

Process finished with exit code 0

通过上述结果,验证了初始化的规则,先父类再子类,先static属性者代码块,再普通属性或者代码块,最后才是构造函数;通过代码块和属性的结果也能看出来,属性和代码块的属性是按照代码顺序来的。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值