Name

Field

Synopsis

This class represents a field of a class. Instances of Field are obtained by calling the getField( ) and related methods of java.lang.Class. Field implements the Member interface, so once you have obtained a Field object, you can use getName( ) , getModifiers( ), and getDeclaringClass( ) to determine the name, modifiers, and class of the field. Additionally, getType( ) returns the type of the field.

The set( ) method sets the value of the represented field for a specified object. (If the represented field is static, no object need be specified, of course.) If the field is of a primitive type, its value can be specified using a wrapper object of type Boolean, Integer, and so on, or it can be set using the setBoolean( ), setInt( ), and related methods. Similarly, the get( ) method queries the value of the represented field for a specified object and returns the field value as an Object. Various other methods query the field value and return it as various primitive types.

In Java 5.0, Field implements AnnotatedElement to support reflection on field annotations. The new getGenericType( ) method supports reflection on the generic type of fields, and isEnumConstant( ) supports fields of enum types.

java.lang.reflect.Field

Figure 10-93. java.lang.reflect.Field

public final class Field extends AccessibleObject implements Member {
// No Constructor
                  // Public Instance Methods public Object ...

Get Java in a Nutshell, 5th Edition 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.