Initialization of a class consists of executing its
staticinitializers and the initializers for
staticfields (class variables) declared in the class.
Therefore, the presence of a static field triggers the initialization of a class. However, the initializer of a static field could depend on the initialization of another class, possibly creating an initialization cycle.
At run time,
staticvariables that are
finaland that are initialized with compile-time constant values are initialized first.
This statement does not apply to instances that use values of static final fields that are initialized at a later stage. Declaring a field to be static final is insufficient to guarantee that it is fully initialized before being read.
Programs in general should—and security-sensitive programs must—eliminate all class initialization cycles.
Noncompliant Code Example (Intraclass Cycle)
This noncompliant code example contains an intraclass initialization cycle:
Cycle class declares a
private static final class variable, which is initialized to a new instance of the
Cycle class. Static initializers are guaranteed to be invoked once before the first use of a static class member or the first invocation of a constructor.
The programmer's intent is to calculate the account balance by subtracting the processing fee from the deposited amount. However, the initialization of the
c class variable happens before the runtime initialization of the
deposit field because it appears lexically before the initialization of the
deposit field. Consequently, the value of
deposit seen by the constructor, when invoked during the static initialization of
c, is the initial value of
deposit (0) rather than the random value. As a result, the balance is always computed to be
Compliant Solution (Intraclass Cycle)
This compliant solution changes the initialization order of the class
Cycle so that the fields are initialized without creating any dependency cycles. Specifically, the initialization of
c is placed lexically after the initialization of
deposit so that it occurs temporally after
deposit is fully initialized.
Such initialization cycles become insidious when many fields are involved, so it is important to ensure that the control flow lacks such cycles.
Although this compliant solution prevents the initialization cycle, it depends on declaration order and is consequently fragile; later maintainers of the software may be unaware that the declaration order must be maintained to preserve correctness. Consequently, such dependencies must be clearly documented in the code.
Noncompliant Code Example (Interclass Cycle)
This noncompliant code example declares two classes with static variables whose values depend on each other. The cycle is obvious when the classes are seen together (as here) but is easy to miss when viewing the classes separately.
The initialization order of the classes can vary, causing computation of different values for
B.b. When class
A is initialized first,
A.a will have the value 2, and
B.b will have the value 1. These values will be reversed when class
B is initialized first.
Compliant Solution (Interclass Cycle)
This compliant solution breaks the interclass cycle by eliminating the dependency of
With the cycle broken, the initial values will always be
A.a = 2 and
B.b = 3 regardless of initialization order.
Noncompliant Code Example
The programmer in this noncompliant code example attempts to initialize a static variable in one class using a static method in a second class, but that method in turn relies on a static method in the first class:
This code correctly initializes
A.a to 1, using the Oracle JVM, regardless of whether
B is loaded first. However, the JLS does not guarantee
A.a to be properly initialized. Furthermore, the initialization cycle makes this system harder to maintain and more likely to break in surprising ways when modified.
This compliant solution moves the
c() method into class
B, breaking the cycle:
Initialization cycles may lead to unexpected results.
|Useless Assignment (Java)|
Useless Assignment to Default (Java)
|CERT.DCL00.ACD||Ensure that files do not contain cyclical dependencies|
|Classes should not access their own subclasses during initialization|
Initialization of Variables [LAV]
CWE-665, Improper Initialization
Puzzle 49, "Larger than Life"
|§12.4, "Initialization of Classes and Interfaces"|
DCL00-J. Prevent class initialization cycles LiveLesson