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();
}
}
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