java.lang.reflect.Field

  • How to retrieve field values using Java reflection

    Values of public member fields in a Java object can be obtained (and are usually obtained) with the notation<objectName>.<fieldName>. But Java offers another possibility to do that: using the reflection API it is possible to retrieve ajava.lang.reflect.Field object representing a given field and to call specific methods of the Field class to obtain field values. While this process is clearly not necessary in the majority of cases where programmers know a priori the definition of classes they are using in a given program, if such definition is not known (or is only partially known) until when the program is executed the reflection API represents a valuable tool for dealing with unknown classes.

    Differently from the usual case where a class definition is available at build time, the fields of a given class accessed through the reflection API can have an arbitrary type, thus the Field class provides a number of methods to retrieve field values of different types; all of these methods take as their unique argument the object from which a field value has to be retrieved. Below are listed the methods that allow retrieving each Java primitive type:

  • How to retrieve the fields of a Java class

    The Java reflection API, i.e. the set of classes defined in thejava.lang.reflect package, offers powerful tools to inspect at run-time the definition of any class used in a Java program. The usual starting point to inspect a class definition is acquiring ajava.lang.Class instance representing a given class; it is easy to obtain such an instance because every class defines implicitly a public field calledclass that contains a reference to the Class object representing this class. Among other things,java.lang.Class allows retrieving the definition of all the accessible public fields of a given class: the method to call to do so isgetFields(), which returns an array ofjava.lang.reflect.Field objects representing each of the (class or instance) fields.

    The Field class represents a particular field defined in a class or interface, and contains various methods to get or set the field value, or to get static information such as field name and type. In the following example we will retrieve the name of a field, using thegetName() method. The code first acquires the Class object corresponding to thejava.lang.String class, then retrieves the fields of the String class with a call to getFields(), and finally prints the name of the first field.

  • How to set field values using Java reflection

    In the Java reflection API, the java.lang.reflect.Field class represents a generic field in a class or interface, and allows to retrieve programmatically field information, such as name, type, or annotations. In addition, via the Field class we can get and set the value of a field in a given object.

    The most generic method to set a field value is set(Object obj, Object value): it takes as first argument the object on which the field needs to be set, and as second argument the value for the field. The above method can be used for any field type, either primitive or class type; in case of a primitive type, value must be an instance of the Java class corresponding to the type, such as java.lang.Integer, java.lang.Float, and so on. For primitive types, there also exist specific methods that can be called passing them directly primitive type values: