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.

import java.awt.Rectangle;
import java.lang.reflect.Field;

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