Reputation: 15
I have an Object that I'm supposed to get the first and last fields for. Normally, I'd use the getters for the fields in question, but since I don't know what those fields are...
I tried the get() method, but apparently that only works on ArrayLists. Anyone know of anything out there that's object/class compatible?
Upvotes: 0
Views: 641
Reputation: 2202
You can retrieve fields’ names and types using Java reflection. Call Class#getDeclaredFields
.
Field[] allFields = Person.class.getDeclaredFields();
Get the first and last elements of that array. Call get…
methods on the Field
class.
Upvotes: 4