public class InitialClass
{
/**
* @param args
*/
public static void main(String[] args)
{
new Z();
}
}
class X
{
static int x = ;
int x2 = ;
Y y = new Y();
public X()
{
System.out.println("X");
}
{
System.out.println("x2 = " + x2);
}
static
{
x = * x;
System.out.println("x = " + x);
}
}
class Y
{
public Y()
{
System.out.println("Y");
}
}
class Z extends X
{
static int z = ;
int z2 = ;
Y y = new Y();
public Z()
{
System.out.println("Z");
}
{
System.out.println("z2 = " + z2);
}
static
{
System.out.println("z = " + z);
}
}
执行结果
x = 2
z = 1
Y
x2 = 2
X
Y
z2 = 2
Z
初始化过程:
1. 初始化父类中的静态成员变量和静态代码块 ;
2. 初始化子类中的静态成员变量和静态代码块 ;
3. 初始化父类的普通成员变量和代码块,再执行父类的构造方法;
4. 初始化子类的普通成员变量和代码块,再执行子类的构造方法;