class S extends Q
{
static
{
System.out.println("S static block");
}
{
System.out.println("S construct block");
}
public S()
{
System.out.println("S constructor");
}
}
运行结果:
P static block
Q static block
S static block
P construct block
P constructor
Q construct block
Q constructor
S construct block
S constructor
P construct block
P constructor
Q construct block
Q constructor
S construct block
S constructor
静态代码块:随着类的加载而加载,只执行一次,优先于主函数,用于给类初始化。作者: 陆强强 时间: 2012-6-19 19:04
//静态代码块,构造函数,构造代码块的执行顺序:e a b d f j b d hallo World!
class ToSequenced
{
static
{
System.out.println("a");//静态代码块优先于其他,且只有被主函数调用的类的静态才会执行,只执行一次 2
}