| 发表于:2007-10-15 15:32:33 楼主 |
这个程序如下所示: class bowl { bowl(int marker) { system.out.println("bowl(" + marker + ")"); } void f(int marker) { system.out.println("f(" + marker + ")"); } } class table { static bowl b1 = new bowl(1); table() { system.out.println("table()"); b2.f(1); } void f2(int marker) { system.out.println("f2(" + marker + ")"); } static bowl b2 = new bowl(2); } class cupboard { bowl b3 = new bowl(3); static bowl b4 = new bowl(4); cupboard() { system.out.println("cupboard()"); b4.f(2); } void f3(int marker) { system.out.println("f3(" + marker + ")"); } static bowl b5 = new bowl(5); } public class staticinitialization { public static void main(string[] args) { system.out.println("creating new cupboard() in main"); new cupboard(); system.out.println("creating new cupboard() in main"); new cupboard(); t2.f2(1); t3.f3(1); } static table t2 = new table(); static cupboard t3 = new cupboard(); } 输出结果是: bowl(1) bowl(2) table() f(1) bowl(4) bowl(5) bowl(3) cupboard() f(2) creating new cupboard() in main bowl(3) cupboard() f(2) creating new cupboard() in main bowl(3) cupboard() f(2) f2(1) f3(1) 我对着这个结果还有疑惑,要问大家,关于“静态初始化”,它的初始化顺序到底是什么??? 对于这个程序而言,从输出结果看,是先从static table t2 = new table(); 这一行代码开始的,这是为什么呢??? 为什么不从其它地方开始??? |
|
|
|
|