子类的实例化主要分为两个步骤:
1.类相关 静态
内容
初始化
先父类再子类:
1.父类的static属性; 2.父类的static块; 3.子类的static属性; 4.子类的static块;
2.类的 实例
内容 初始化;
调用构造子
先父类后子类:构造子调用之前先实例化实例字段和块
5.父类的普通属性; 6.父类的普通块; 7.父类的构造子; 8.子类的普通属性; 9.子类的普通块; 10.子类的构造子;
举例:
package basic; public class ClzInit { public static void main(String[] args) { new Son(); } } class Parent { protected int n = 5; protected static int m = 5; static { m = m * 2; System.out.println("父类静态块调用; m="+m); } { n = n * 2; m = m * 2; System.out.print("父类普通块调用;"); System.out.print("n="+n); System.out.println(" m="+m); } public Parent() { this.n = n * 10; m = m + 10; System.out.print("父类构造子; n="+n); System.out.println(" m="+m); } } class Son extends Parent { private int sn=3; private static int sm=3; static { m = m + 2; sm=sm+2; System.out.println("子类静态块调用; m="+m); } { n = n + 2; sn=sn+2; m = m + 2; System.out.println("子类普通块调用;"); System.out.print("n="+n); System.out.print("sn="+sn); System.out.println(" m="+m); } public Son() { this.n = n + 10; sn=sn+10; m = m + 10; System.out.print("子类构造子;n="+n); System.out.println(" m="+m); } }
看输出结果,可见端倪:
父类静态块调用; m=10
子类静态块调用; m=12
父类普通块调用;n=10 m=24
父类构造子; n=100 m=34
子类普通块调用;
n=102sn=5 m=36
子类构造子;n=112 m=46
[2011-10-26]备忘。