Java的块、静态块、构造函数,继承执行的先后顺序

class A {
	  static {
	    	 System.out.println("static output of A");
	     }
	  {
		  System.out.println("not static output of A");
	  }
	public A(){
		System.out.println("constructor output of A");
	}
}

class B extends A {
	  static {
	    	 System.out.println("static output of B");
	     }
	  {
		  System.out.println("not static output of B");
	  }
	public B(){
		System.out.println("constructor output of B");
	}
}


 class C {
	  static {
	    	 System.out.println("static output of C");
	     }
	  {
		  System.out.println("not static output of C");
	  }
	public C(){
		System.out.println("constructor output of C");
	}

	public static void main(String[] args){
		 B b = new B();
                   System.out.println("------------------------");
		 B b2 = new B();
	}
}


输出为:
static output of C
static output of A
static output of B
not static output of A
constructor output of A
not static output of B
constructor output of B
------------------------
not static output of A
constructor output of A
not static output of B
constructor output of B


详细解释可见以下文章:
http://d-y-zh.iteye.com/blog/607027
http://blog.csdn.net/superbeck/archive/2009/02/22/3920693.aspx

你可能感兴趣的:(java,C++,c,Blog,C#)