O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required