Java Field.getInt(Object obj)

Syntax

Field.getInt(Object obj) has the following syntax.

public int getInt(Object obj)  throws IllegalArgumentException ,   IllegalAccessException

Example

In the following code shows how to use Field.getInt(Object obj) method.


import java.lang.reflect.Field;
/*w w w  .j a v  a  2  s  .  com*/
class X {
  public int i = 10;
  public static final double PI = 3.14;
}

public class Main {
  public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("X");
    X x = (X) clazz.newInstance();
    Field f = clazz.getField("i");
    System.out.println(f.getInt(x)); // Output: 10
    f.setInt(x, 20);
    System.out.println(f.getInt(x)); // Output: 20
    f = clazz.getField("PI");
    System.out.println(f.getDouble(null)); // Output: 3.14
    f.setDouble(x, 20);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable