结论:执行顺序静态代码块->{}->构造器执行顺序问题
/**
* @description: 此代码讨论静态代码块-{}-构造器执行顺序问题
* @create: 2021-11-17 16:52
*/
public class StaticExaple {
public static String staticString = "Static Variable";
{
System.out.println("This is normal block");
}
static {
System.out.println("This is static block");
}
public StaticExaple(){
System.out.println("This is constrructor");
}
static {
System.out.println("This is second static block and " + staticString);
}
static {
staticMethod();
System.out.println("This is third static block");
}
public static void staticMethod(){
System.out.println("This is static method");
}
public static void staticMethod2(){
System.out.println("This is static method2");
}
public static void main(String[] args) {
StaticSubClass staticSubClass = new StaticSubClass();
staticSubClass.staticMethod2();
}
}
class StaticSubClass extends StaticExaple{
static{
System.out.println("This is subclass static block");
}
{
System.out.println("This is the initialise block in subclass");
}
public StaticSubClass(){
System.out.println("This is the subclass contructor");
}
}
//限制性父类静态代码块
/* This is static block
This is second static block and Static Variable
This is static method
This is third static block*/
//在执行子类静态代码块
/*This is subclass static block*/
//在执行父类{}
/*This is normal block*/
//在执行父类构造器
/*This is constrructor*/
//在执行子类{}
/*This is the initialise block in subclass*/
//在执行子类构造器
/*This is the subclass contructor*/
//在执行子类方法
/*This is static method2*/