Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: grammar changes only

...

...at run time, static variables that are final and that are initialized with compile-time constant values are initialized first.

While this statement typically holds, it This statement can be misleading as it does not account for because it is inapplicable to instances that use values of static final fields that are initialized at a later stage. Even when Declaring a field is to be static final, is insufficient to guarantee that it is not necessarily fully initialized before being read.

...

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 deposit field is initialized because it is 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 equal computed to be -10.

Wiki Markup
The JLS permits implementations to ignore the possibility of such recursive attempts \[[Bloch 2005|AA. Bibliography#Bloch 05]\].  

...

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.

...

This noncompliant code example declares two classes with static variables whose values depend on each other. When seen together, the The cycle is obvious when the classes are seen together (as here), but the cycle can easily be easily missed when the classes are viewed separately.

...

The initialization order of the classes can vary, and consequently compute cause computation of different values for A.a and 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.

...