Java get property value by property name

You can use some of the Libraries that offer property-based access. I think the most known and used is beanutils. You can find one good example of the beanutils "in action" here. Some sample code:

A someBean = new A();

// access properties as Map
Map<String, Object> properties = BeanUtils.describe(someBean);
properties.set("name","Fred");
BeanUtils.populate(someBean, properties);

// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny"); 

Yes. You can replace the commented out line with t.getClass().getField(map.get(key)).get(t). which will retrieve the value of the field on t.


bjc2406's answer works fine as long as the field(s) in question are accessible:

t.getClass().getField(map.get(key)).get(t)

If you can't reasonably make it public, reflection and other field access APIs should get the job done: How do I read a private field in Java?