The full process of initialization : Initialization Block « Class Definition « Java Tutorial






class Insect {
  private int i = 1;

  protected int j;

  Insect() {
    System.out.println("i = " + i + ", j = " + j);
    j = 1;
  }

  private static int x1 = print("static Insect.x1 initialized");

  static int print(String s) {
    System.out.println(s);
    return 0;
  }
}

class Beetle extends Insect {
  private int k = print("Beetle.k initialized");

  public Beetle() {
    System.out.println("k = " + k);
    System.out.println("j = " + j);
  }

  private static int x2 = print("static Beetle.x2 initialized");
}

public class MainClass {
  public static void main(String[] args) {
    Beetle b = new Beetle();
  }

}
static Insect.x1 initialized
static Beetle.x2 initialized
i = 1, j = 0
Beetle.k initialized
k = 0
j = 1








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