The Java Developers Almanac 1.4


Order this book from Amazon.

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

e114. Getting the Field Objects of a Class Object

There are three ways of obtaining a Field object from a Class object.
    Class cls = java.awt.Point.class;
    
    // By obtaining a list of all declared fields.
    Field[] fields = cls.getDeclaredFields();
    
    // By obtaining a list of all public fields, both declared and inherited.
    fields = cls.getFields();
    for (int i=0; i<fields.length; i++) {
        Class type = fields[i].getType();
        process(fields[i]);
    }
    
    // By obtaining a particular Field object.
    // This example retrieves java.awt.Point.x.
    try {
        Field field = cls.getField("x");
        process(field);
    } catch (NoSuchFieldException e) {
    }

 Related Examples
e115. Getting and Setting the Value of a Field

See also: Arrays    Constructors    Methods    Modifiers   


© 2002 Addison-Wesley.