以下代码:
class A
{

A()
{
System.out.println("A constructor no args");
}

A(int i)
{
System.out.println("A constructor " + i);
}
}class B extends A
{
static A a =
{
new A(55);
System.out.println("static A var ");
};
B(int i)
{
System.out.println("B constructor " + i);
}
B()
{
super(88);
System.out.println("B constructor " + 88);
}

}class C extends B
{
static B b = 
{
new B();
System.out.println("static B var ");
};
C(int i)
{
super(i);
System.out.println("C constructor "+i);
}

public static void main(String [] args)
{
C c = new C(5);
}
}B类中的静态变量a应该只初始化一次才对啊.但是实际运行结果如下:A constructor no args
A constructor 55
static A var 
B constructor 5
A constructor 88
A constructor 55
static A var 
B constructor 88
static B var 
C constructor 5也就是说B类中的静态变量a被初始化了两次?请各位高手指点.

解决方案 »

  1.   

    static A a =
    {
    new A(55);
    System.out.println("static A var ");
    };
    里面不是调用 new A(55); 了吗
      

  2.   

    thinking in java第三版.第四章.里面有你想要的东西.
    我也是看的这个.不错的东西!
      

  3.   

    A()
    {
    System.out.println("A constructor no args");
    }
    上面的构造器是如何被调用的,我怎么看来看去都象是只有
    A(int i)
    {
    System.out.println("A constructor " + i);
    }
    这个被调用
      

  4.   

    我也是正在看thinking in java中..另外这段代码是在Eclipse中编译通过的,直接用javac无法编译。不懂中...To frilly(秋◆水): 静态变量只是初始化一次的.To chuan122345(Synchronized+ObjectPool) 能说的明白一点么?