Java代码实际执行顺序

来源:互联网 发布:落英纷飞 音乐软件 编辑:程序博客网 时间:2024/06/06 02:25
package Test;//静态对象(变量)优先 非静态对象(变量)  父类优先于子类   按照成员定义顺序进行初始化//java 执行顺序:父类静态变量,父类静态代码块,子类静态变量,子类静态代码块,父类非静态变量,父类非静态代码块//父类构造函数,子类非静态变量,子类非静态代码块,子类构造函数。class Parent{static{System.out.println("Parent static code1");}{System.out.println("Parent base code block");}public Parent(){System.out.println("Parent constructor");}static{System.out.println("Parent static code2");}}public class JavaStatic extends Parent{static{System.out.println("Child static code");}{System.out.println("Child base code block");}public JavaStatic(){System.out.println("Child constructor");}public static void main(String[] args){new JavaStatic();}}

运行结果为:
Parent static code1
Parent static code2
Child static code
Parent base code block
Parent constructor
Child base code block
Child constructor