public class A {
static{
System.out.println("a static");
}
public A(){
System.out.println("a construct");
}
}public class B extends A {
static {
System.out.println("b static");
}

public B(){
System.out.println("b construct");
}
}public class Main {
public static void main(String[] args) {
A ab=new B();
ab=new B(); }}
输出后的结果为
a static
b static
a construct
b construct
a construct
b construct
请问各位大虾为什么会是这个结果哪

解决方案 »

  1.   

    static是类加载时就执行的,不管有没有实例化这个类,把上面的改为A ab=new A();
     ab= new B();
    输出是
    a static
    b static
    a construct
    a construct
    b construct
      

  2.   

    static静态块在类加载时执行.A类被加载,B类被加载.然后调用构造函数,子类构造函数,要先调用父类的构造函数.所以A构造子执行,B构造子执行.再次附值的时候同理....
      

  3.   

    考点
    1。static
    2。子类,构造函数。
      

  4.   

    回复人:grass_12(java你好) ( 一级(初级)) 信誉:100  2007-7-10 10:58:02  得分:0
    ?  static是类加载时就执行的,不管有没有实例化这个类,把上面的改为A ab=new A();
    ab= new B();
    输出是
    a static
    b static
    a construct
    a construct
    b construct顺序应该是:
    a static
    a construct
    b static
    a construct
    b construct
      

  5.   

    static在类被加载的时候就会自动调用 而且只会调用一次。
    static区块比构造方法更先执行
    在实例化子类的对象的时候  会自动调用父类的构造方法
      

  6.   

    顺序 static->父类构造函数->子类构造函数