| |
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) {
}
e115.
Getting and Setting the Value of a Field
© 2002 Addison-Wesley.
|