Java Reference
In-Depth Information
is not enough that
setAccessible(true)
would succeed, it must actually
be called.
This capability is provided for highly specialized contexts and is not in-
tended for general usewe mention it only for completeness. Changing
a final field can have unexpected, possibly tragic consequences unless
performed in specific contexts, such as custom deserialization. Outside
those contexts, changes to a final field are not guaranteed to be visible.
Even in such contexts, code using this technique must be guaranteed
that security will not thwart its work. Changing a final field that is a con-
stant variable (see page
46
) will never result in the change being seen,
except through the use of reflectiondon't do it!
Exercise 16.6
: Create an
Interpret
program that creates an object of a
requested type and allows the user to examine and modify fields of that
object.