How to get value of String field from Java reflection?

后端 未结 3 1894
猫巷女王i
猫巷女王i 2021-01-22 10:21

I have an object that has a String field. I can obtain this field by calling:

Field field = someObj.getClass().getField(\"strField\");

I sett a

3条回答
  •  耶瑟儿~
    2021-01-22 11:24

    Easiest way is to use BeanUtils:

    String s = BeanUtils.getProperty(someObj, "strField");
    

    Note that BeanUtils will attempt to convert your property into string. You need to have a getter and setter of the property

提交回复
热议问题