Mutation methods

A number of methods in Java SE API provide write access to non-static final fields. This means that Java code can alter the value of a final field after the field has been initialized in a constructor. The methods that provide write access, known as mutation methods are:

The use of mutation methods to alter the values of final fields is strongly inadvisable because it undermines the correctness of programs written in expectation of final fields being immutable.

In the reference implementation, a module can be granted the capability to mutate final instance fields of normal classes in packages that are open to the module using the command line option {@code --enable-reflective-final-mutation=M1,M2, ... M} where {@code M1}, {@code M2}, {@code ...Mn} are module names (for the unnamed module, the special value {@code ALL-UNNAMED} can be used). Mutation of final instance fields of normal classes from modules not listed by that option is deemed illegal. The command line option {@code --illegal-reflective-final-mutation} controls how illegal reflective final mutation is handled. Valid values for this command line option are "warn", "allow", and "deny". If this option is not specified then the default is "warn" so that illegal reflective final mutation will result in a warning at runtime.