Matthew Earlywine
Matthew Earlywine

Reputation: 15

Get first and last Fields in a Java Object

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

Answers (1)

Sergey Afinogenov
Sergey Afinogenov

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

Related Questions