How to set field values using Java reflection

In the Java reflection API, the java.lang.reflect.Field class represents a generic field in a class or interface, and allows to retrieve programmatically field information, such as name, type, or annotations. In addition, via the Field class we can get and set the value of a field in a given object.

The most generic method to set a field value is set(Object obj, Object value): it takes as first argument the object on which the field needs to be set, and as second argument the value for the field. The above method can be used for any field type, either primitive or class type; in case of a primitive type, value must be an instance of the Java class corresponding to the type, such as java.lang.Integerjava.lang.Float, and so on. For primitive types, there also exist specific methods that can be called passing them directly primitive type values:

·         setBoolean(Object obj, boolean z)

·         setByte(Object obj, byte b)

·         setChar(Object obj, char c)

·         setDouble(Object obj, double d)

·         setFloat(Object obj, float f)

·         setInt(Object obj, int i)

·         setLong(Object obj, long l)

·         setShort(Object obj, short s)

As with the generic set() method, in all the methods in the above list the first argument is the object whose field needs to be set and the second argument indicates the value for the field.

In the example below, we create an object of the java.awt.Rectangle class and then set field values in that object by using the setInt() method; to verify that field values are actually set, we retrieve these values, using the getInt(Object obj) method of the Field class, before and after setting them.

import java.awt.*;import java.lang.reflect.*;public class SetFieldValueExample {  public static void main(String[] args) throws Exception {    Rectangle rect = new Rectangle();    Field xField = rect.getClass().getField("x");    Field yField = rect.getClass().getField("y");      Field widthField = rect.getClass().getField("width");    Field heightField = rect.getClass().getField("height");    System.out.println("---->> Before setting values <<----");    System.out.println("x value: " + xField.getInt(rect));    System.out.println("y value: " + yField.getInt(rect));    System.out.println("width value: " + widthField.getInt(rect));    System.out.println("height value: " + heightField.getInt(rect));    xField.setInt(rect, 10);    yField.setInt(rect, 20);    widthField.setInt(rect, 40);    heightField.setInt(rect, 80);    System.out.println("---->> After setting values <<----");    System.out.println("x value: " + xField.getInt(rect));    System.out.println("y value: " + yField.getInt(rect));      System.out.println("width value: " + widthField.getInt(rect));    System.out.println("height value: " + heightField.getInt(rect));  }}

The output of the above program demonstrates that all four field values have been correctly set:

 

 ---->> Before setting values <<----

 x field value: 0
 y field value: 0
 width field value: 0

 height field value: 0

 ---->> After setting values <<----

 x field value: 10
 y field value: 20
 width field value: 40

 height field value: 80

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值