Initialization order : Initialization Block « Class Definition « Java Tutorial






class MyClass {
  MyClass(int marker) {
    System.out.println("Tag(" + marker + ")");
  }
}

class MyInit {
  MyClass t1 = new MyClass(1); // Before constructor

  MyInit() {
    System.out.println("Card()");
    t3 = new MyClass(33);
  }

  MyClass t2 = new MyClass(2); // After constructor

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

  MyClass t3 = new MyClass(3); // At end
}

public class MainClass {
  public static void main(String[] args) {
    MyInit t = new MyInit();
    t.f();
  }
}
Tag(1)
Tag(2)
Tag(3)
Card()
Tag(33)
f()








5.11.Initialization Block
5.11.1.A class that contains a static initializer:
5.11.2.Initializing Data Members
5.11.3.Initialization order
5.11.4.Using Initialization Blocks: A non-static initialization block
5.11.5.Using Initialization Blocks: static initialization block
5.11.6.Mixed Initializer
5.11.7.Explicit static initialization with the static clause
5.11.8.The full process of initialization
5.11.9.Demonstrates various initializers
5.11.10.Demonstrates the pitfalls of depending on the order of static initializers