Field: get(Object obj) : Field « java.lang.reflect « Java by API






Field: get(Object obj)

 
import java.lang.reflect.Field;
import java.util.Date;

public class Main {
  public static void main(String[] args) throws Exception {
    Bean demo = new Bean();
    Class clazz = demo.getClass();

    Field field = clazz.getField("id");
    field.set(demo, new Long(10));
    Object value = field.get(demo);
    System.out.println("Value = " + value);

    field = clazz.getField("now");
    field.set(null, new Date());
    value = field.get(null);
    System.out.println("Value = " + value);
  }

}

class Bean {
  public static Date now;

  public Long id;

  public String name;

}

   
  








Related examples in the same category

1.Field: getAnnotation(Class annotationClass)
2.Field: getDouble(Object obj)
3.Field: getGenericType()
4.Field: getInt(Object obj)
5.Field: getModifiers()
6.Field: Class getType()
7.Field: isEnumConstant()
8.Field: isSynthetic()
9.Field: setAccessible(boolean flag)
10.Field: setBoolean(Object obj, boolean z)
11.Field: setInt(Object obj, int i)