2

Possible Duplicate:
java: get all variable names in a class

How can I read out all fields and their content of an Object o by using the reflection api. I would begin with o.getClass().getDeclaredFields() but don't know how to continue.

Anyone got an idea ?

Community
  • 1
  • 1

1 Answers1

5

o.getClass().getDeclaredFields() will return an array of Field instances. For each Field f in your array, you can access it's value with f.get(o).


Edit Thank you to @MarkoTopolnik for pointing this out: if you plan to read non-public fields, be sure to first invoke f.setAccessible(true) before getting their values.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • I'll try it tomorrow but so far this seems to be the answer i was looking for. Thanks ! –  Oct 07 '12 at 18:57
  • One important tip, though: if you plan to read non-public fields, be sure to first invoke `f.setAccessible(true)` before getting its value. @A.R.S. if I may suggest, add this to your answer. – Marko Topolnik Oct 07 '12 at 19:55