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 want to get the value of that field then we can use this method to return the value of Field.
Syntax:
public int getInt(Object obj)
throws IllegalArgumentException,
IllegalAccessException
Parameters: This method accepts a single parameter obj which is the object to extract the int value from.
Return value: This method returns the value of field converted to type int.
Exception: This method throws following Exception:
- IllegalAccessException: if Field object is enforcing Java language access control and the underlying field is inaccessible.
- IllegalArgumentException: if the specified object is not an instance of the class or interface declaring the underlying field or if the field value cannot be converted to the type int by a widening conversion.
- 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 getInt() method:
Program 1:
// Java program to demonstrate getInt() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // Create the Employee class object Employee Employee = new Employee(); // Get the Salary field object Field field = Employee.class.getField("Salary"); // Apply getInt Method on Employee Object // to get the value of Salary field int value = field.getInt(Employee); // print result System.out.println("Value of int Field" + " Salary is " + value); } } // sample Employee class class Employee { // static int values public static int Salary = 34113; public static String name = "Aman"; public static int getSalary() { return Salary; } public static void setSalary(int Salary) { Employee.Salary = Salary; } public static String getName() { return name; } public static void setName(String name) { Employee.name = name; } } |
Value of int Field Salary is 34113
Program 2:
// Java program to demonstrate getInt() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // Create the IntNumbers class object IntNumbers Int = new IntNumbers(); // Get the value field object Field field = IntNumbers.class.getField("value"); // Apply getInt Method on field Object // to get the value of value field int value = field.getInt(Int); // print result System.out.println("Value: " + value); } // IntNumbers class static class IntNumbers { // int field public static int value = 999994567; // getter and setter methods public static int getValue() { return value; } public static void setValue(int value) { IntNumbers.value = value; } } } |
Value: 999994567
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getInt-java.lang.Object-
Recommended Posts:
- ByteBuffer getInt() method in Java with Examples
- Field get() method in Java with Examples
- Field set() method in Java with Examples
- Array getInt() Method in Java
- Field setChar() method in Java with Examples
- Field setDouble() method in Java with Examples
- Field setFloat() method in Java with Examples
- Field setInt() method in Java with Examples
- Field setByte() method in Java with Examples
- Field getModifiers() method in Java with Examples
- Field toGenericString() method in Java with Examples
- Field getAnnotation() method in Java With Examples
- Field getDeclaredAnnotations() method in Java With Examples
- Field isEnumConstant() method in Java with Examples
- Field getAnnotationsByType() 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.



