java.lang.Float

  • 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: