When a parent class references a member of a subclass during its own initialization, the results might not be what you expect because the child class might not have been initialized yet. This could create what is known as an "initialisation cycle", or even a deadlock in some extreme cases.


To make things worse, these issues are very hard to diagnose so it is highly recommended you avoid creating this kind of dependencies.

== Noncompliant Code Example

----
class Parent {
  static int field1 = Child.method(); // Noncompliant
  static int field2 = 42;

  public static void main(String[] args) {
    System.out.println(Parent.field1); // will display "0" instead of "42"
  }
}

class Child extends Parent {
  static int method() {
    return Parent.field2;
  }
}
----

== See

* https://www.securecoding.cert.org/confluence/display/java/DCL00-J.+Prevent+class+initialization+cycles[CERT, DCL00-J.] - Prevent class initialization cycles
* Java Language Specifications - https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4[Section 12.4: Initialization of Classes and Interfaces]
