java 构造器调用_java构造方法的调用

这段代码展示了Java中类的加载和初始化顺序。首先,静态块`staticfatherblock`和`staticsonblock`按顺序执行,然后是实例化过程,包括父类构造器`newFather()`和子类构造器`newson()`,以及对应的块代码。中间没有方法名的代码块实际上是构造器的初始化块,它们在构造器之前执行。
摘要由CSDN通过智能技术生成

classA{staticAa1=newA(111);publicA(inti){System.out.println("newFather()"+i);}{System.out.println("fatherblock");}static{System.out.println("staticfatherblock");}}publicc...

class A{

static A a1=new A(111);

public A(int i){

System.out.println("new Father()"+i);

}

{

System.out.println("father block");

}

static {

System.out.println("static father block");

}

}

public class Init extends A{

static Init a2=new Init(222);

Init(){

this(12);

System.out.println("new son()");

}

Init(int x){

super(0);

System.out.println("new son(int)");

}

{

System.out.println("son block");

}

static {

System.out.println("static son block");

}

public static void main(String[] args){

new Init();

}

}

这段代码的结果是:

father block

new Father()111

static father block

father block

new Father()0

son block

new son(int)

static son block

father block

new Father()0

son block

new son(int)

new son()

为什么会出现这种调用循序,而且中间没有方法名的方法怎么也可以执行?

展开

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值