The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.lang.reflect  [17 examples] > Fields  [2 examples]

e115. Getting and Setting the Value of a Field

This example assumes that the field has the type int.
    try {
        // Get value
        field.getInt(object);
    
        // Set value
        field.setInt(object, 123);
    
        // Get value of a static field
        field.getInt(null);
    
        // Set value of a static field
        field.setInt(null, 123);
    } catch (IllegalAccessException e) {
    }

 Related Examples
e114. Getting the Field Objects of a Class Object

See also: Arrays    Constructors    Methods    Modifiers   


© 2002 Addison-Wesley.