mercredi 27 mai 2015

Java reflection field returning wrong value

I am trying to get a field and its value dynamically by passing in the name of the field as a string. The values are coming out wrong. When I tried to get a String, the value is null. Clearly the field for the variable I am getting is not null or invalid since I just printed it out. Any ideas?

Field field = person.getClass().getDeclaredField("mUserId");
                                field.setAccessible(true);


System.out.println("key: " + key + " field: " + field.getInt(person)
                            + " actual value " + person.getUserId());

public class Person extends someclass {
    public int mUserId;

    public int getUserId() {
        fetch();
        return mUserId;
    }
}

output

key: mUserId field: 0 actual value 388925





Aucun commentaire:

Enregistrer un commentaire