这个程序如下所示:class Bowl {
Bowl(int er) {
System.out.println("Bowl(" + er + ")");
}
void f(int er) {
System.out.println("f(" + er + ")");
}
}class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int er) {
System.out.println("f2(" + er + ")");
}
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 er) {
System.out.println("f3(" + er + ")");
}
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(); 这一行代码开始的,这是为什么呢???为什么不从其它地方开始???
Bowl(int er) {
System.out.println("Bowl(" + er + ")");
}
void f(int er) {
System.out.println("f(" + er + ")");
}
}class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int er) {
System.out.println("f2(" + er + ")");
}
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 er) {
System.out.println("f3(" + er + ")");
}
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(); 这一行代码开始的,这是为什么呢???为什么不从其它地方开始???
Cup(){}
Cup(int er) {
System.out.println("Cup(" + er + ")");
}
void f(int er) {
System.out.println("f(" + er + ")");
}
}class Cups {
static Cup c1;
static Cup c2;
static {
c1 = new Cup(1);
c2 = new Cup(2);
}
Cups() {
System.out.println("Cups()");
}
}public class ExplicitStatic {
public static void main(String[] args) {
System.out.println("Inside main()");
}
Cup x = new Cup(13);
static Cups y = new Cups();
}
输出结果是:Cup(1)
Cup(2)
Cups()
Inside main()
我的问题是: 既然书上说“静态”对象的初始化顺序先于“非静态”对象的初始化, 那么根据这个推理,Cup x = new Cup(13)这一行应该在static Cups y = new Cups() 初始化后得到初始化,调用 Cup类的构造方法,输出结果应该是:Cup(1)
Cup(2)
Cups()
Inside main()
Cup(13)但是:我编译后,为什么不出现这个结果呢???也就说为什么Cup x = new Cup(13)这一行没有得到初始化???
苦思不得其解,还希望有人能够帮助我,谢谢了。
“对于非继承类,是先从public类的static成员开始初始化起的,且static成员只初始化一次”
Cup x = new Cup(13); x不是static的,只有在创建对象时才会先于构造器被加载。