package test;
class Super {
static int x;
static {
System.out.println("Initialization Super");
}
}
class Sub extends Super {
static final int y = 2005;
static int z;
static {
System.out.println("Initialization Sub");
}
}
public class Initialization {
public static void main(String[] args) {
System.out.println(Sub.x);
// 不会引起Sub类的初始化,因为x是定义在Super类中的
System.out.println("-------------------------");
System.out.println(Sub.y);
// 不会引起Sub类的初始化,因为y是常量
System.out.println("-------------------------");
System.out.println(Sub.z = 2004);
// 将会引起Sub的初始化
}
}
//结果:
//*****************************
//Initialization Super
//0
//-------------------------
//2005
//-------------------------
//Initialization Sub
//2004