构造器初始化(static)

 package demo;

 /*
* 在类 的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,
* 它们仍旧会在任何方法(包括构造器)被调用之前得到初始化。
*/
public class Test {
public static void main(String[] args) {
House h = new House();
h.f();
}
} class Window {
Window(int maker) {
System.out.println("Window(" + maker + ")");
}
} class House {
Window w1 = new Window(1); House() {
System.out.println("House()");
w3 = new Window(33);
} Window w2 = new Window(2); void f() {
System.out.println("f()");
} Window w3 = new Window(3);
}
//结果:
//Window(1)
//Window(2)
//Window(3)
//House()
//Window(33)
//f()
 package demo;

 /*
* 先执行static修饰的成员,而且只被执行一次
*/
public class Test1 {
public static void main(String[] args) {
System.out.println("Creating new Cup() in main");
new Cup();
System.out.println("Creating new Cup() in main");
new Cup();
table.f2(1);
cup.f3(1);
} static Table table = new Table();
static Cup cup = new Cup();
} class Bowl {
Bowl(int maker) {
System.out.println("Bowl(" + maker + ")");
} void f1(int maker) {
System.out.println("f1(" + maker + ")");
}
} class Table {
static Bowl bowl1 = new Bowl(1); Table() {
System.out.println("Table()");
bowl1.f1(1);
} void f2(int maker) {
System.out.println("f2(" + maker + ")");
} static Bowl bowl2 = new Bowl(2);
} class Cup {
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4); Cup() {
System.out.println("Cup()");
bowl4.f1(2);
} void f3(int maker) {
System.out.println("f3(" + maker + ")");
} static Bowl bowl5 = new Bowl(5);
}
结果:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cup()
f1(2)
Creating new Cup() in main
Bowl(3)
Cup()
f1(2)
Creating new Cup() in main
Bowl(3)
Cup()
f1(2)
f2(1)
f3(1)
上一篇:类成员(static)和final修饰符


下一篇:Linux下oracle11gR2系统安装到数据库建立配置及最后oracle的dmp文件导入一站式操作记录