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

- {@link java.lang.reflect.Field#set(Object, Object)}
- {@link java.lang.reflect.Field#setBoolean(Object, boolean)}
- {@link java.lang.reflect.Field#setByte(Object, byte)}
- {@link java.lang.reflect.Field#setChar(Object, char)}
- {@link java.lang.reflect.Field#setInt(Object, int)}
- {@link java.lang.reflect.Field#setLong(Object, long)}
- {@link java.lang.reflect.Field#setFloat(Object, float)}
- {@link java.lang.reflect.Field#setDouble(Object, double)}
- {@link java.lang.invoke.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.