| |
7. 4. 7. Reflection, Introspection, and Naming |
|
import java.lang.reflect.Field;
public class RectangleFields {
public static void main(String args[]) {
Class rectClass = null;
Field rectField[] = null;
try {
rectClass = Class.forName("java.awt.Rectangle");
rectField = rectClass.getDeclaredFields();
} catch (SecurityException se) {
System.out.println("Access to Rectangle fields denied");
} catch (ClassNotFoundException cnfe) {
System.out.println("Didn't find the Rectangle class");
}
System.out.println("Fields in Rectangle are:");
for (int i = 0; i < rectField.length; i++) {
System.out.println(rectField[i].toString());
}
}
}
|
|
|