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)
我有点搞不懂,从这个程序的执行结果来看,我弄不明白这个程序的执行过程,期待高手帮我解决,不甚感激!
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 Cupboard t3 = new Cupboard();这两行先于main函数而执行,因为是静态的成员而new Table对象的时候,又有两个静态成员的初始化
static Bowl b1 = new Bowl(1);
static Bowl b2 = new Bowl(2);
依次分析吧
第一步:
初始化静态成员变量和静态成员函数
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
a:先执行static Table t2 = new Table();
进入类Table,初始化static Bowl b1 = new Bowl(1);和static Bowl b2 = new Bowl(2);
先执行static Bowl b1 = new Bowl(1);
进入类Bowl,执行构造函数,输出Bowl(1);
然后执行static Bowl b2 = new Bowl(2);
进入类Bowl,执行构造函数,输出Bowl(2);
接着执行Table的构造函数,输出Table();
最后在构造函数中调用了b2.f(1),那么就输出f(1);
此时执行完static Table t2 = new Table();
程序跳到步骤b,执行static Cupboard t3 = new Cupboard();
b: 执行顺序是依次为
static Bowl b4 = new Bowl(4);
static Bowl b5 = new Bowl(5);
Bowl b3 = new Bowl(3);
Cupboard()
在执行这四个过程中依次输出Bowl(4)、Bowl(5)、Bowl(3)、Cupboard()、f(2);
到这里为止,主类中的2个静态成员已经初始化完毕;
接着就依次执行主类中的
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 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);
}
里边定义了静态的成员变量,就先于这个类里边的其他成员方法运行呢?比如说上面:先执行static Bowl b1 = new Bowl(1);紧接着执行static Bowl b2 = new Bowl(2);,哪为什么不紧接着执行
Table() {
System.out.println("Table()");
b2.f(1);
}呢?是不是有这样的先后顺序?