class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f1() {
System.out.println("Ok");
}
}
class Cupboard {
Bowl b1 = new Bowl(1);
static Bowl b2 = new Bowl(2);
Cupboard() {
System.out.println("Cupboard()");
}
static Bowl b3 = new Bowl(3);
}
class Table {
Table() {
System.out.println("Table()");
}
Table(String a, int i) {
this();
System.out.println("Ok");
}
}
public class Order {
static Cupboard t1 = new Cupboard();
static Table t2;
Bowl t3 = new Bowl(10);
void print() {
t3.f1();
}
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
Order od = new Order();
od.t3.f1();
Table t4 = new Table("aa", 1);
}
}
输出结果:
Bowl(2)
Bowl(3)
Bowl(1)
Cupboard()
Creating new Cupboard() in main
Bowl(1)
Cupboard()
Creating new Cupboard() in main
Bowl(10)
Ok
Table()
Ok
static属性只初始化一次。