有关静态初始化块说法正确的是?
java对象初始化顺序
先说结论:
注意点:
代码测试:
/** * Description: Java对象初始化顺序 * Created by yangyz on 2018/12/25 */ class Father { public Father() { System.out.println("父类无参构造方法"); } static { System.out.println("父类静态代码块1"); } private static int a = Help.fatherStaticMemberVarInit(); static { System.out.println("父类静态代码块2"); } { System.out.println("父类普通代码块1"); } private int b = Help.fatherMemberVarInit(); { System.out.println("父类普通代码块2"); } public Father(int v) { System.out.println("父类带参构造方法"); } } class Son extends Father { static { System.out.println("子类静态代码块1"); } private static int a = Help.sonStaticMemberVarInit(); static { System.out.println("子类静态代码块2"); } { System.out.println("子类普通代码块1"); } private int b = Help.sonMemberVarInit(); { System.out.println("子类普通代码块2"); } public Son() { // super(2018); System.out.println("子类构造方法"); } } class Help { public static int fatherStaticMemberVarInit() { System.out.println("父类静态成员变量"); return 0; } public static int fatherMemberVarInit() { System.out.println("父类普通成员变量"); return 0; } public static int sonStaticMemberVarInit() { System.out.println("子类静态成员变量"); return 0; } public static int sonMemberVarInit() { System.out.println("子类普通成员变量"); return 0; } } public class Test { public static void main(String[] args) { Son son1 = new Son(); System.out.println("==================="); Son son2 = new Son(); } }
测试结果:
static { // 初始化内容 }4. 如果有多个静态块,分别写在代码的不同位置,算不算控制了执行时间?