初始化

成员方法的初始化

  • 在类的内部,变量的定义顺序决定了初始化的顺序。创建一个对象时,会先初始化对象中的成员变量,然后才会初始化方法(包括构造器)。
  • 成员变量 ==> 方法
    x
class Window {
    Window (int marker) {
        System.out.println("Window: " + marker);
    }
}
class House {
    Window w1 = new Window(1);
    House () {
        System.out.println("init house");
        w3 = new Window(4);
    }
    Window w2 = new Window(2);
    Window w3 = new Window(3);
}
public class OrderOfInitialization {
    public static void main (String[] args) {
        House house = new House();
    }
}
$ javac OrderOfInitialization.java 
$ java OrderOfInitialization
Window: 1
Window: 2
Window: 3
init house
Window: 4

静态成员的初始化

  • 静态成员变量的初始化要优先于成员变量,但是静态成员变量只有在必要的时候才会初始化。
  • 如果不创建对象,或者不通过类名直接调用,静态成员永远也不会初始化。
  • 静态成员变量只会初始化一次,就是在第一次创建对象时,后面无论创建多少次该对象,都不会再次初始化。
  • 静态成员变量 ==> 成员变量 ==> 方法
class Bowl {
    Bowl(int marker) {
        System.out.println("Bowl class marker: " + marker);
    }
    void method(int marker) {
        System.out.println("Bowl method: " + marker);
    }
}

class Table {
    static Bowl bowl1 = new Bowl(1);
    Table() {
        System.out.println("Table class marker");
        bowl2.method(1);
    }
    void method(int marker) {
        System.out.println("Table method: " + marker);
    }
    static Bowl bowl2 = new Bowl(2);
}

class Cupboard {
    Bowl bowl3 = new Bowl(3);
    static Bowl bowl4 = new Bowl(4);
    Cupboard() {
        System.out.println("Cupboard class marker");
        bowl4.method(1);
    }
    void method(int marker) {
        System.out.println("Cupboard method: " + 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.method(1);
        cupboard.method(1);
    }
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
}
$ javac StaticInitialization.java
$ java StaticInitialization
Bowl class marker: 1
Bowl class marker: 2
Table class marker
Bowl method: 1
Bowl class marker: 4
Bowl class marker: 5
Bowl class marker: 3
Cupboard class marker
Bowl method: 1
Creating new Cupboard in main
Bowl class marker: 3
Cupboard class marker
Bowl method: 1
Creating new Cupboard in main
Bowl class marker: 3
Cupboard class marker
Bowl method: 1
Table method: 1
Cupboard method: 1

显式的静态初始化

  • 静态代码块初始化的优先级要高于静态变量,只要对该类发送了消息。
  • 无论是通过new关键字对该类进行了初始化,还是通过Cups.cup1直接调用,静态代码块都会进行初始化。
class Cup {
    Cup(int marker) {
        System.out.println("Cup class marker: " + marker);
    }
    void method(int marker){
        System.out.println("Cup method: " + marker);
    }
}
class Cups {
    static Cup cup1;
    static Cup cup2;
    static {
        cup1 = new Cup(1);
        cup2 = new Cup(2);
    }
    Cups (){
        System.out.println("Cup class");
    }
}
public class ExplicitStatic {
    public static void main(String[] args) {
        System.out.println("Inside main");
        Cups.cup1.method(1);
    }
    //static Cups cups1 = new Cups();
    //static Cups cups2 = new Cups();
}
$ javac ExplicitStatic.java
$ java ExplicitStatic
Inside main
Cup class marker: 1
Cup class marker: 2
Cup method: 1

你可能感兴趣的:(初始化)