next up previous contents index
Next: if, switch, and synchronized Up: Field declarations Previous: Field declarations   Contents   Index

Interfaces.

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, $v$, of an interface requires simplification, we create a nested class $C$. Add a field of the same name, type, and initial value in $C$. Transform the declaration of the class field as described in the previous section. Then replace the initializer of the interface field $v$ with $C$.$v$.



Bryan Carpenter 2003-04-15