当前位置: 技术问答>java相关
静态的初始化
来源: 互联网 发布时间:2015-02-18
本文导语: 谁能给讲讲下面的程序,谢谢!class Bowl { Bowl(int marker) { System.out.println("Bowl(" + marker + ")"); } void f(int marker) { System.out.println("f(" + marker + ")"); } } class Table { static Bowl b1 = new Bowl(...
谁能给讲讲下面的程序,谢谢!class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
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 marker) {
System.out.println("f3(" + marker + ")");
}
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 marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
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 marker) {
System.out.println("f3(" + marker + ")");
}
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)
请问:为什么会这样,能讲详细点行吗?
|
静态变量初始化是在类被初次调用的瞬间发生的,类中的非静态变量是在进入构造函数内部前初始化的,记住这两点,就可以很清楚地理解你的输出
1. static Table t2 = new Table();
第一次调用Table
==>static Bowl b1 = new Bowl(1);
==>static Bowl b2 = new Bowl(2);
==>Table() {
System.out.println("Table()");
b2.f(1);
}
2. static Cupboard t3 = new Cupboard();
第一次调用Cupboard
==>static Bowl b4 = new Bowl(4);
==>static Bowl b5 = new Bowl(5);
==>Cupboard()
==>Bowl b3 = new Bowl(3);
==>{
System.out.println("Cupboard()");
b4.f(2);
}
3. System.out.println("Creating new Cupboard() in main");
4. new Cupboard();
==>Cupboard()
==>Bowl b3 = new Bowl(3);
==>{
System.out.println("Cupboard()");
b4.f(2);
}
5. System.out.println( "Creating new Cupboard() in main");
6. new Cupboard();
==>Cupboard()
==>Bowl b3 = new Bowl(3);
==>{
System.out.println("Cupboard()");
b4.f(2);
}
7. t2.f2(1);
8. t3.f3(1);
1. static Table t2 = new Table();
第一次调用Table
==>static Bowl b1 = new Bowl(1);
==>static Bowl b2 = new Bowl(2);
==>Table() {
System.out.println("Table()");
b2.f(1);
}
2. static Cupboard t3 = new Cupboard();
第一次调用Cupboard
==>static Bowl b4 = new Bowl(4);
==>static Bowl b5 = new Bowl(5);
==>Cupboard()
==>Bowl b3 = new Bowl(3);
==>{
System.out.println("Cupboard()");
b4.f(2);
}
3. System.out.println("Creating new Cupboard() in main");
4. new Cupboard();
==>Cupboard()
==>Bowl b3 = new Bowl(3);
==>{
System.out.println("Cupboard()");
b4.f(2);
}
5. System.out.println( "Creating new Cupboard() in main");
6. new Cupboard();
==>Cupboard()
==>Bowl b3 = new Bowl(3);
==>{
System.out.println("Cupboard()");
b4.f(2);
}
7. t2.f2(1);
8. t3.f3(1);
|
因为一个类别加载的时候就已经初始化所有的静态成员了,
要运行main函数首先就要加载这个类的
要运行main函数首先就要加载这个类的
|
当涉及到一个类时总会生成静态成员;然后若要生成这个类的实例,再调用initializer(默认的或人为创建的);调用构件器;完成创建。若运行一个类则会在初始化完静态成员后进入main这个入口,不会创建类的实例,除非在main的实现里有new出实例的代码。
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。