Field setInt() method in Java with Examples
setInt() method of java.lang.reflect.Field used to set the value of a field as an int on the specified object. When you need to set the value of a field of an object as int then you can use this method to set value over an Object.
Syntax:
public void setInt(Object obj, int i)
throws IllegalArgumentException,
IllegalAccessException
Parameters: This method accepts accepts two parameters:
- obj: which is the object whose field should be modified and
- i: 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 implementor thereof), or if an unwrapping conversion fails.
- NullPointerException: if the specified object is null and the field is an instance field.
- ExceptionInInitializerError: if the initialization provoked by this method fails.
Below programs illustrate setInt() method:
Program 1:
// Java program to illustrate setInt() 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 salary System.out.println( "Value of salary before " + "applying setInt is " + emp.salary); // Get the field object Field field = Employee.class.getField("salary"); // Apply setInt Method field.setInt(emp, 2243599); // print value of salary System.out.println( "Value of salary after " + "applying setInt is " + emp.salary); // print value of uniqueNo System.out.println( "Value of uniqueNo before " + "applying setInt is " + emp.uniqueNo); // Get the field object field = Employee.class.getField("uniqueNo"); // Apply setInt Method field.setInt(emp, 123434); // print value of uniqueNo System.out.println( "Value of uniqueNo after " + "applying setInt is " + emp.uniqueNo); } } // sample class class Employee { // static int values public static int uniqueNo = 234289; public static int salary = 1125213; } |
Value of salary before applying setInt is 1125213 Value of salary after applying setInt is 2243599 Value of uniqueNo before applying setInt is 234289 Value of uniqueNo after applying setInt is 123434
Program 2:
// Java program to illustrate setInt() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create Numbers object Numbers no = new Numbers(); // Get the value field object Field field = Numbers.class .getField("value"); // Apply setInt Method field.setInt(no, 53266); // print value of isActive System.out.println( "Value after " + "applying setInt is " + Numbers.value); } } // sample Numbers class class Numbers { // static int value public static int value = 13685; } |
Value after applying setInt is 53266
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setInt-java.lang.Object-int-
Recommended Posts:
- Array setInt() method in Java
- Field set() method in Java with Examples
- Field get() method in Java with Examples
- Field setLong() method in Java with Examples
- Field setByte() method in Java with Examples
- Field getShort() method in Java with Examples
- Field getName() method in Java with Examples
- Field getInt() method in Java with Examples
- Field getLong() method in Java with Examples
- Field hashCode() method in Java with Examples
- Field getAnnotation() method in Java With Examples
- Field isSynthetic() method in Java with Examples
- Field setShort() method in Java with Examples
- Field toGenericString() method in Java with Examples
- Field setDouble() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



