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. 初始化子類的普通成員變量和代碼塊,再執行子類的構造方法;