Setting Field Values

String newTitle = "President";
											Class c = someObject.getClass();
											Field titleField = c.getField("title");
											titleField.set(someObject, newTitle);

In order to set a field value, you must first get a Field object for the field you want to set the value of. See the phrase “Discovering Class Fields” in this chapter for more information about getting Field objects from a class. You should also refer to the phrase “Getting Field Values” also contained in this chapter for more information about getting field values.

The Field class has specialized methods for setting the values of primitive types. Methods such as setInt(), setFloat(), and setByte() allow you to set the values of the primitive types. For complete details of the set ...

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.