Open In App

Field set() method in Java with Examples

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.

Syntax:



public void set(Object obj, Object value)
         throws IllegalArgumentException,
                IllegalAccessException

Parameters: This method accepts  two parameters:

Return: This method returns nothing. Exception: This method throws the following Exception:



Below programs illustrate set() method: Program 1: 




// 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;
}

Output:
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 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;
}

Output:
Values after applying set are { false, 1213, 342414.131 }

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#set-java.lang.Object-java.lang.Object-


Article Tags :