Field (Java Platform SE 8 ) (original) (raw)
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 aNullPointerException
. 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 is enforcing Java language access control, and the underlying field is inaccessible, the method throws anIllegalAccessException
.
If the underlying field is final, the method throws anIllegalAccessException
unless setAccessible(true)
has succeeded for this Field
object and the 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 anIllegalArgumentException
.
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 anIllegalArgumentException
.
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.