Aracılığıyla paylaş


Field.Set(Object, Object) Method

Definition

Sets the field represented by this Field object on the specified object argument to the specified new value.

[Android.Runtime.Register("set", "(Ljava/lang/Object;Ljava/lang/Object;)V", "")]
public void Set (Java.Lang.Object? obj, Java.Lang.Object? value);
[<Android.Runtime.Register("set", "(Ljava/lang/Object;Ljava/lang/Object;)V", "")>]
member this.Set : Java.Lang.Object * Java.Lang.Object -> unit

Parameters

obj
Object

the object whose field should be modified

value
Object

the new value for the field of obj being modified

Attributes

Exceptions

if the object is null and the field is non-static

if the object is not compatible with the declaring class

if this field is not accessible

Remarks

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 is enforcing Java language access control, and the underlying field is inaccessible, the method throws an IllegalAccessException.

If the underlying field is final, this Field object has <em>write</em> access if and only if the following conditions are met: <ul> <li>#setAccessible(boolean) setAccessible(true) has succeeded for this Field object;</li> <li>the field is non-static; and</li> <li>the field's declaring class is not a Class#isRecord() record class.</li> </ul> If any of the above checks is not met, this method throws an IllegalAccessException.

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.

Java documentation for java.lang.reflect.Field.set(java.lang.Object, java.lang.Object).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to