Get/Set field value
In this chapter you will learn:
- How to get value from a field
- How to set field value with Reflection
- Value for a public final static field cannot be set through Java reflection
Get value from a field
We can get field value with reflection methods and they returns primitive types. The following lists have all methods we need to know.
Object get(Object obj)
Returns field valueboolean 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.
import java.awt.Rectangle;
import java.lang.reflect.Field;
/*java 2 s .co m*/
public class Main {
public static void main(String[] args) {
Rectangle r = new Rectangle(100, 325);
Class c = r.getClass();
try {
Field heightField = c.getField("height");
Integer heightValue = (Integer) heightField.get(r);
System.out.println("Height: " + heightValue.toString());
} catch (NoSuchFieldException e) {
System.out.println(e);
} catch (SecurityException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
}
}
}
The output:
Set field value with Reflection
We can also set the value back to a field 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.
import java.awt.Rectangle;
import java.lang.reflect.Field;
/* ja v a 2s. c o m*/
public class Main {
public static void main(String[] args) {
Rectangle r = new Rectangle(100, 325);
Class c = r.getClass();
try {
Field heightField = c.getField("height");
heightField.setInt(r, 1000);
Integer heightValue = (Integer) heightField.get(r);
System.out.println("Height: " + heightValue.toString());
} catch (NoSuchFieldException e) {
System.out.println(e);
} catch (SecurityException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
}
}
}
Get/set field value and possible exceptions
The following code gets and sets the values of instance and class fields
import java.lang.reflect.Field;
/*from ja v a 2 s. c om*/
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);
}
}
Here is the output from the code above. The exception is for the final static field PI
.
public final static
field defines a constant in Java and we cannot reset a value for a
constant through reflection.
Next chapter...
What you will learn in the next chapter:
- Using java.lang.reflect.Method to do method reflection
- Find out Method's return type, modifiers and parameters
- How to invoke a method