public Object get(Object obj)
throws IllegalArgumentException,
IllegalAccessException
Returns the value of the field represented by this
Field
, on the specified object. The value is automatically wrapped in an object if it has a primitive type.
public class GetExample { private String aString = "an instance string"; private int anInt = 10; private static BigDecimal anObj = BigDecimal.TEN;
public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field field = GetExample.class.getDeclaredField("aString"); GetExample instance = new GetExample(); Object o = field.get(instance); System.out.println(o);
//primitive instance field Field field2 = GetExample.class.getDeclaredField("anInt"); Object o2 = field2.get(instance); System.out.println(o2.getClass().getTypeName()); System.out.println(o2);
//static field Field field3 = GetExample.class.getDeclaredField("anObj"); Object o3 = field3.get(null); System.out.println(o3.getClass().getTypeName()); System.out.println(o3); } }
Output
an instance string java.lang.Integer 10 java.math.BigDecimal 10