Java Reflection Java Java API
java.lang.reflect.Field
public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException
Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.
int
package com.logicbig.example.field;import java.lang.reflect.Field;public class GetIntExample { private int i = 10; public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field field = GetIntExample.class.getDeclaredField("i"); int fieldValue = field.getInt(new GetIntExample()); System.out.println(fieldValue); }}
10
Widening conversion from byte to int:
package com.logicbig.example.field;import java.lang.reflect.Field;public class GetIntExample2 { private byte b = 20; public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field field = GetIntExample2.class.getDeclaredField("b"); int fieldValue = field.getInt(new GetIntExample2()); System.out.println(fieldValue); }}
20
Static field example:
package com.logicbig.example.field;import java.lang.reflect.Field;public class GetIntExample3 { private static int i = 100; public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field field = GetIntExample3.class.getDeclaredField("i"); int fieldValue = field.getInt(new GetIntExample3()); System.out.println(fieldValue); }}
100