Mutation methods (Java SE 26 & JDK 26)

# 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:

- `Field.set(Object, Object)`
- `Field.setBoolean(Object, boolean)`
- `Field.setByte(Object, byte)`
- `Field.setChar(Object, char)`
- `Field.setInt(Object, int)`
- `Field.setLong(Object, long)`
- `Field.setFloat(Object, float)`
- `Field.setDouble(Object, double)`
- `MethodHandles.Lookup.unreflectSetter(java.lang.reflect.Field)`

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 classes in packages that are open to the module using the command line option `--enable-final-field-mutation=M1,M2, ... Mn}` where `M1`, `M2`, `...Mn` are module names (for the unnamed module, the special value `ALL-UNNAMED` can be used). Mutation of final instance fields of classes from modules not listed by that option is deemed illegal. The command line option `--illegal-final-field-mutation` controls how illegal final mutation is handled. Valid values for this command line option are "warn", "allow", "debug, and "deny". If this option is not specified then the default is "warn" so that illegal final field mutation will result in a warning at runtime.