Sets the field represented by this
Field
object on the
specified object argument to the specified new value. The new
value is automatically unwrapped if the underlying field has a
primitive type.
The operation proceeds as follows:
If the underlying field is static, the obj
argument is
ignored; it may be null.
Otherwise the underlying field is an instance field. If the
specified object argument is null, the method throws a
NullPointerException
. If the specified object argument is not
an instance of the class or interface declaring the underlying
field, the method throws an IllegalArgumentException
.
If this Field
object enforces Java language access control, and
the underlying field is inaccessible, the method throws an
IllegalAccessException
.
If the underlying field is final, the method throws an
IllegalAccessException
unless
setAccessible(true)
has succeeded for this field
and this field is non-static. Setting a final field in this way
is meaningful only during deserialization or reconstruction of
instances of classes with blank final fields, before they are
made available for access by other parts of a program. Use in
any other context may have unpredictable effects, including cases
in which other parts of a program continue to use the original
value of this field.
If the underlying field is of a primitive type, an unwrapping
conversion is attempted to convert the new value to a value of
a primitive type. If this attempt fails, the method throws an
IllegalArgumentException
.
If, after possible unwrapping, the new value cannot be
converted to the type of the underlying field by an identity or
widening conversion, the method throws an
IllegalArgumentException
.
If the underlying field is static, the class that declared the
field is initialized if it has not already been initialized.
The field is set to the possibly unwrapped and widened new value.
If the field is hidden in the type of obj
,
the field's value is set according to the preceding rules.
Parameters:
- obj - the object whose field should be modified
- value - the new value for the field of obj
being modified
Throws:
- IllegalAccessException - if the underlying field
is inaccessible.
- IllegalArgumentException - if the specified object is not an
instance of the class or interface declaring the underlying
field (or a subclass or implementor thereof),
or if an unwrapping conversion fails.
- NullPointerException - if the specified object is null
and the field is an instance field.
- ExceptionInInitializerError - if the initialization provoked
by this method fails.