The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is static, the obj argument is ignored; it may be null Otherwise, the underlying field is an instance field.
- This method throws different exception depending upon scenarios like this method throws a NullPointerException if the specified object argument is null
- or IllegalArgumentException if the specified object argument is not an instance of the class or interface declaring the underlying field.
- This method throws an IllegalAccessException if this Field object is enforcing Java language access control, and the underlying field is inaccessible.
- This method throws an IllegalArgumentException 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. This 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.
- If the field is static and if it has not already been initialized then the class that declared this field is 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.
Syntax:
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException
Parameters: This method accepts two parameters:
- obj: which is the object whose field should be modified and
- value: which is the new value for the field of obj being modified.
Return: This method returns nothing. Exception: This method throws the following Exception:
- IllegalAccessException: if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
- IllegalArgumentException: if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementer thereof), or if an unwrapping conversion fails.
- NullPointerException: if the specified object is null and the field is an instance field.
- ExceptionInitializerError: if the initialization provoked by this method fails.
Below programs illustrate set() method: Program 1:
Java
// Java program illustrate set() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create user object Employee emp = new Employee(); // print value of uniqueNo System.out.println( "Value of uniqueNo before " + "applying set is " + emp.uniqueNo); // Get the field object Field field = Employee. class .getField("uniqueNo"); // Apply set Method field.set(emp, ( short ) 1213 ); // print value of uniqueNo System.out.println( "Value of uniqueNo after " + "applying set is " + emp.uniqueNo); // print value of salary System.out.println( "Value of salary before " + "applying set is " + emp.salary); // Get the field object field = Employee. class .getField("salary"); // Apply set Method field.set(emp, 324344.2323 ); // print value of salary System.out.println( "Value of salary after " + "applying set is " + emp.salary); } } // sample class class Employee { // static values public static short uniqueNo = 239 ; public static double salary = 121324.13333 ; } |
Value of uniqueNo before applying set is 239 Value of uniqueNo after applying set is 1213 Value of salary before applying set is 121324.13333 Value of salary after applying set is 324344.2323
Program 2:
Java
// Java program illustrate set() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws AccessException { // create attributes object attributes att = new attributes(); // Get the value field object Field field1 = attributes. class .getField("bolValue"); Field field2 = attributes. class .getField("intValue"); Field field3 = attributes. class .getField("doubleValue"); // Apply set Method field1.set(att, false ); field2.set(att, 1213 ); field3.set(att, 342414.131 ); // print value of isActive System.out.println( "Values after " + "applying set are { " + att.bolValue + ", " + att.intValue + ", " + att.doubleValue + " }."); } } // sample attributes class class attributes { // static value value public static boolean bolValue = false ; public static int intValue = 13134 ; public static double doubleValue = 1314.141 ; } |
Values after applying set are { false, 1213, 342414.131 }