构造器初始化

  1. 初始化顺序

  在类的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,它们仍旧会在任何方法(包括构造器)被调用之前得到初始化。

public class House {
    Window w1 = new Window(1);  // Before constructor

    House() {
        // show that we're in the constructor
        System.out.println("House");
        Window w3 = new Window(33);
    }

    Window w2 = new Window(2); // After constructor

    void f() {
        System.out.println("f()");
    }

    Window w3 = new Window(3); // At end


    public static void main(String[] args) {
        House h = new House();
        h.f(); // Shows that constructor is done
    }
}
public class Window {
    Window(int marker) {
        System.out.println("window(" + marker + ")");
    }
}

结果:

window(1)
window(2)
window(3)
House
window(33)
f()

  2. 静态数据的初始化

  无论创建多少个对象,静态数据都只占用一份存储区域。

public class Bowl {
    Bowl(int marker){
        System.out.println("Bowl(" + marker +")");
    }
    void f1(int marker){
        System.out.println("f1(" + marker + ")");
    }
}
public class Table {
    static Bowl bowl1 = new Bowl(1);
    Table(){
        System.out.println("Table()");
        bowl2.f1(1);
    }
    void f2(int marker){
        System.out.println("f2(" + marker + ")");
    }

    static Bowl bowl2 = new Bowl(2);
 }
public class Cupboard {
    Bowl bowl3 = new Bowl(3);  // 每次初始化时,都会调用
    static Bowl bowl4 = new Bowl(4);  // 只调用一次
    Cupboard(){
        System.out.println("Cupboard()");
        bowl4.f1(2);
    }
    void f3(int marker){
        System.out.println("f3(" + marker + ")");
    }
    static Bowl bowl5 = 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();
        table.f2(1);
        cupboard.f3(1);
    }
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
}

结果:

Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)

  3. 显式的静态初始化

  cups的静态初始化只会执行一次。

public class Cup {
    Cup(int marker){
        System.out.println("Cup(" + marker + ")");
    }

    void f(int marker){
        System.out.println("f(" + marker + ")");
    }
}
public class Cups {
    static Cup cup1;
    static Cup cup2;
    static {
        cup1 = new Cup(1);
        cup2 = new Cup(2);
    }
    Cups(){
        System.out.println("Cups");
    }
}
public class ExplicitStatic {
    public static void main(String[] args) {
        System.out.println("Inside main()");
         Cups.cup1.f(99); // (1)
    }
     // static Cups cups1 = new Cups(); // (2)
     // static Cups cups2 = new Cups(); // (2)
}

结果:

Inside main()
Cup(1)
Cup(2)
f(99)

  

  4. 非静态实例初始化

public class Mug {
    Mug(int marker){
        System.out.println("Mug(" + marker + ")");
    }
    void f(int marker){
        System.out.println("f(" + marker + ")");
    }
}
public class Mugs {
    Mug mug1;
    Mug mug2;
    {
        mug1 = new Mug(1);
        mug2 = new Mug(2);
        System.out.println("mug1 & mug2 initialized");
    }

    Mugs(){
        System.out.println("Mugs()");
    }

    Mugs(int i){
        System.out.println("Mugs(int)");
    }

    public static void main(String[] args) {
        System.out.println("Inside main()");
        new Mugs();
        System.out.println("new Mugs() completed");
        new Mugs(1);
        System.out.println("new Mugs(1) completed");
    }
}

结果:

Inside main()
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs()
new Mugs() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs(int)
new Mugs(1) completed

 

posted @ 2021-09-25 16:36  慕仙白  阅读(141)  评论(0)    收藏  举报