Getting Field Values

Class c = anObject.getClass();
											Field titleField = c.getField("title");
											String titleVal = (String) titleField.get(anObject);

In order to get a field value, you must first get a Field object for the field you want to obtain the value of. See the phrase “Discovering Class Fields” earlier in this chapter for more information about getting Field objects from a class.

The Field class has specialized methods for getting the values of primitive types. Methods such as getInt(), getFloat(), and getByte() allow you to get the values of the primitive types. For complete details of the getter methods available on the Field object, see the JavaDoc at: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Field.html.

To get fields ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.