Field Reflection

A Field provides information about a field of a class or an interface.

Get the field modifiers, name and type

int getModifiers()
Returns the Java language modifiers for the field.
String getName()
Returns the name of the field.
Class<?> getType()
Returns a Class object that identifies the declared type.

Is this field an Enum, is it Synthetic

boolean isEnumConstant()
Returns true if this field represents an element of an enumerated type; returns false otherwise.
boolean isSynthetic()
Returns true if this field is a synthetic field; returns false otherwise.

Get annotation for this field

<T extends Annotation>T getAnnotation(Class<T> annotationClass)
Returns this element's annotation.
Annotation[] getDeclaredAnnotations()
Returns all annotations that are directly present on this element.

Get the class for this field

Class<?> getDeclaringClass()
Returns the Class object representing the class or interface that declares the field.
Object get(Object obj)
Returns field value
boolean getBoolean(Object obj)
Gets field value as boolean.
byte getByte(Object obj)
Gets field value as byte.
char getChar(Object obj)
Gets field value as char.
double getDouble(Object obj)
Gets field value as double.
float getFloat(Object obj)
Gets field value as float.
Type getGenericType()
Returns a Type object that represents the declared type for the field represented by this Field object.
int getInt(Object obj)
Gets field value as int.
long getLong(Object obj)
Gets field value as long.
short getShort(Object obj)
Gets field value as short.

Set field value with Reflection

void set(Object obj, Object value)
Sets the field value.
void setBoolean(Object obj, boolean z)
Sets boolean field value.
void setByte(Object obj, byte b)
Sets byte field value.
void setChar(Object obj, char c)
Sets char field value.
void setDouble(Object obj, double d)
Sets double field value.
void setFloat(Object obj, float f)
Sets float field value.
void setInt(Object obj, int i)
Sets int field value.
void setLong(Object obj, long l)
Sets long field value.
void setShort(Object obj, short s)
Sets short field value.

Get the string representation of a field

String toGenericString()
Returns a string describing this Field, including its generic type.
String toString()
Returns a string describing this Field.

Revised from Open JDK source code

The following code gets and sets the values of instance and class fields

 
import java.lang.reflect.Field;

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);
  }
}
  

10
20
3.14
Exception in thread "main" java.lang.IllegalAccessException: Can not set static final double field X.PI to (double)20.0
  at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:55)
  at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:91)
  at sun.reflect.UnsafeQualifiedStaticDoubleFieldAccessorImpl.setDouble(UnsafeQualifiedStaticDoubleFieldAccessorImpl.java:141)
  at java.lang.reflect.Field.setDouble(Field.java:889)
  at Main.main(Main.java:18)
Home 
  Java Book 
    Reflection  

Field:
  1. Field Reflection
  2. Get the field modifiers, name and type
  3. Get field value with Reflection
  4. Set field value with Reflection