Referencing a static member of a subclass from its parent during class initialization, makes the code more fragile and prone to future bugs. The
execution of the program will rely heavily on the order of initialization of classes and their static members.
What is the potential impact?
This could create what is known as an "initialization cycle", or even a deadlock in some extreme cases. Additionally, if the order of the static
class members is changed, the behavior of the program might change. These issues can be very hard to diagnose so it is highly recommended to 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;
}
}