[問題] 關於物件初始化順序 static與 non-static
小弟最近在 K "Thinking in JAVA 4/e中文版"這本書
裡面第 157頁, 有個地方一直不太理解, 希望高手能幫忙解答
class Bowl{
Bowl(int marker){
System.out.println("Bowl(" + marker + ")");
}
void f1(int marker){
System.out.println("f1(" + marker + ")");
}
}
class Table{
static Bowl bowl1=new Bowl(1);
Table(){
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int marker){
System.out.println("f2(" + marker + ")");
}
static Bowl bowl2=new Bowl(2);
Cupboard test=new Cupboard();
}
class Cupboard{
Bowl bowl3=new Bowl(3);
static Bowl bowl4=new Bowl(4);
Cupboard(){
System.out.println("Cupboard()");
bowl4.f1(2);
}
void f3(int marker){
System.out.println("f3(" + marker + ")");
}
static Bowl bowl5=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();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
後面附了說明
"Bowl 讓你得以觀看 class的誕生過程, Table和 Cupboard會在它們自身的 class
定義式中產生 Bowl static 成員. 請記住, Cupboard會在 static定義之前先產生
non-static Bowl bowl3."
這段是什麼意思? 因為我怎麼試都試不出
Bowl(3)
Bowl(4)
Bowl(5)
的順序
先謝過~
感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.71.19
→
11/03 16:25, , 1F
11/03 16:25, 1F
→
11/03 16:27, , 2F
11/03 16:27, 2F
→
11/03 16:30, , 3F
11/03 16:30, 3F
推
11/03 16:35, , 4F
11/03 16:35, 4F
→
11/03 17:11, , 5F
11/03 17:11, 5F
→
11/03 17:14, , 6F
11/03 17:14, 6F
→
11/03 17:17, , 7F
11/03 17:17, 7F
※ 編輯: eala 來自: 140.114.71.19 (11/03 17:17)