Java declares the calling order of static statement block, instance code block and constructor method when subclassing

I wrote a before, but only for a class. When two classes are encountered, of course, there is an inheritance relationship between the two classes. So when declaring a subclass, what is the calling relationship between the three?

1. Test code:

class Base{
//静态语句块
static{
Sy@R_404_2354@.out.println("执行基类的静态语句块!");
}
//构造器方法
Base(){
Sy@R_404_2354@.out.println("执行基类的构造器方法!");
}
//构造代码块
{
Sy@R_404_2354@.out.println("执行基类的构造代码块!");
}
}
public class TempTest extends Base {
//静态语句块
static{
Sy@R_404_2354@.out.println("执行子类的静态语句块!");
}
//构造器方法
Temptest(){
Sy@R_404_2354@.out.println("执行子类的构造器方法!");
}
//构造代码块
{
Sy@R_404_2354@.out.println("执行子类的构造代码块!");
}
public static void main(String[] args){
TempTest a=new Temptest();
Sy@R_404_2354@.out.println("----------------");
TempTest b=new Temptest();
}
}

2. Test results:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>