Why is this an issue?
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;
}
}
Resources