Field set() method in Java with Examples
Last Updated :
10 Jan, 2023
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
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
Employee emp = new Employee();
System.out.println(
"Value of uniqueNo before "
+ "applying set is "
+ emp.uniqueNo);
Field field
= Employee. class
.getField("uniqueNo");
field.set(emp, ( short ) 1213 );
System.out.println(
"Value of uniqueNo after "
+ "applying set is "
+ emp.uniqueNo);
System.out.println(
"Value of salary before "
+ "applying set is "
+ emp.salary);
field = Employee. class .getField("salary");
field.set(emp, 324344.2323 );
System.out.println(
"Value of salary after "
+ "applying set is "
+ emp.salary);
}
}
class Employee {
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
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws AccessException
{
attributes att = new attributes();
Field field1
= attributes. class
.getField("bolValue");
Field field2
= attributes. class
.getField("intValue");
Field field3
= attributes. class
.getField("doubleValue");
field1.set(att, false );
field2.set(att, 1213 );
field3.set(att, 342414.131 );
System.out.println(
"Values after "
+ "applying set are { "
+ att.bolValue + ", "
+ att.intValue
+ ", " + att.doubleValue
+ " }.");
}
}
class attributes {
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-
Share your thoughts in the comments
Please Login to comment...