How to get string value from a Java field via reflection?

后端 未结 5 602
北海茫月
北海茫月 2020-12-09 16:28

I have a method:

public void extractStringFromField(Class classToInspect) {
    Field[] allFields = classToInspect.getDeclaredFields();

    for(Fie         


        
5条回答
  •  粉色の甜心
    2020-12-09 17:09

    It looks like you need a reference to an instance of the class. You would want to call get and pass in the reference, casting the return to a String.

    You can use get as follows:

    String strValue = (String) field.get (objectReference);
    

提交回复
热议问题