• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding Values in object using Reflections  RSS feed

 
Arulanand Dayalan
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need to know whether this is possible first of all. I have object which is populated with data. I need to do some operations on few fields present in the object. The fields that need to be operated is decided dynamically by reading from an XML file. So how can get an value by only having the field name.

Thanks in Advance.

Regards,
Arul.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.getField(String) or Class.getDeclaredField(String) are your friends.
Of course visibility rules apply.

If the class follow standard JavaBeans naming convention for fields, you can turn the name into the name of an accessor method easily and use Class.getMethod(String, Class[]) instead.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!