Field setInt() method in Java with Examples Last Updated : 10 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 two parameters: obj: which is the object whose field should be modified andi: 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.ExceptionInInitializerError: if the initialization provoked by this method fails. Below programs illustrate setInt() method: Program 1: Java // 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; } Output: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 // 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; } Output: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- Comment More infoAdvertise with us Next Article Field getInt() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions java-lang-reflect-package Java-Field Practice Tags : Java Similar Reads 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 s 4 min read Field get() method in Java with Examples The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an 3 min read Field getInt() method in Java with Examples The getInt() method of java.lang.reflect.Field used to get the value of int which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type int via a widening conversion. When a class contains a static or instance int field and we w 3 min read OptionalInt of(int) method in Java with examples The of(int) method help us to get an OptionalInt object which contains a int value which is passed as a parameter to this method. Syntax: public static OptionalInt of(int value) Parameters: This method accepts a int value as parameter that will be set to the returned OptionalInt object. Return value 1 min read Scanner nextInt() method in Java with Examples The nextInt(radix) method of java.util.Scanner class scans the next token of the input as a Int. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextInt(radix) where the radix is assumed to be the 5 min read Scanner nextInt() method in Java with Examples The nextInt(radix) method of java.util.Scanner class scans the next token of the input as a Int. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextInt(radix) where the radix is assumed to be the 5 min read Like