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详细解释可见以下文章:
[url]http://d-y-zh.iteye.com/blog/607027[/url]
[url]http://blog.csdn.net/superbeck/archive/2009/02/22/3920693.aspx[/url]
本文通过具体的Java代码示例展示了类继承中构造函数与静态、非静态初始化块的执行顺序。解析了不同情况下这些初始化操作如何按特定顺序执行。
1万+

被折叠的 条评论
为什么被折叠?



