The fields of interfaces must be static and final. These fields are often used as to hold constants, but the Java language does not impose any special restrictions on what kind of expressions can appear as variable initializers in interfaces. In particular the variable initializer may require simplification according to our rules.
We cannot perform the precomputation and assignment of the field in an initializer block of the interface, because this kind of initializer is not allowed in interfaces. However we can create a nested private, static member class in an interface, and compute variable initializers in static initializer blocks of the nested class.
So, if the initializer of any field,
, of an interface requires
simplification, we create a nested class
. Add a field
of the same name, type, and initial value in
. Transform
the declaration of the class field as described in the previous
section. Then replace the initializer of the interface field
with ![]()
.
.