class Row{
Row(int count){
System.out.println("Row("+count+")");
}
void f1(){
System.out.println("f1()");
}
}
class Table{
static Row row1 = new Row(1);
Table(){
System.out.println("Table()");
row2.f1();
}
void f2(){
System.out.println("f2()");
}
static Row row2 = new Row(2);
}
class Cupboard{
Row row3 = new Row(3);
static Row row4 = new Row(4);
Cupboard(){
System.out.println("Cupboard()");
row4.f1();
}
void f3(){
System.out.println("f3()");
}
static Row row5 = new Row(5);
}
public class StaticInit{
public static void main(String[] args){
System.out.println("Create new Cupboard");
new Cupboard();
System.out.println("Create new Cupboard");
new Cupboard();
table.f2();
cupboard.f3();
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
这是运行结果:
Row(1)
Row(2)
Table()
f1()
Row(4)
Row(5)
Row(3)
Cupboard()
f1()
Create new Cupboard
Row(3)
Cupboard()
f1()
Create new Cupboard
Row(3)
Cupboard()
f1()
f2()
f3()
Row(int count){
System.out.println("Row("+count+")");
}
void f1(){
System.out.println("f1()");
}
}
class Table{
static Row row1 = new Row(1);
Table(){
System.out.println("Table()");
row2.f1();
}
void f2(){
System.out.println("f2()");
}
static Row row2 = new Row(2);
}
class Cupboard{
Row row3 = new Row(3);
static Row row4 = new Row(4);
Cupboard(){
System.out.println("Cupboard()");
row4.f1();
}
void f3(){
System.out.println("f3()");
}
static Row row5 = new Row(5);
}
public class StaticInit{
public static void main(String[] args){
System.out.println("Create new Cupboard");
new Cupboard();
System.out.println("Create new Cupboard");
new Cupboard();
table.f2();
cupboard.f3();
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
这是运行结果:
Row(1)
Row(2)
Table()
f1()
Row(4)
Row(5)
Row(3)
Cupboard()
f1()
Create new Cupboard
Row(3)
Cupboard()
f1()
Create new Cupboard
Row(3)
Cupboard()
f1()
f2()
f3()