2006-12-21

Object Reflection: get field value : Reflection : Language Basics : Java examples (example source code)

Object Reflection: get field value : Reflection : Language Basics : Java examples (example source code) Organized by topic: "
import java.awt.Rectangle;
import java.lang.reflect.Field;

public class SampleGet {

public static void main(String[] args) {
Rectangle r = new Rectangle(100, 325);
printHeight(r);

}

static void printHeight(Rectangle r) {
Field heightField;
Integer heightValue;
Class c = r.getClass();
try {
heightField = c.getField('height');
heightValue = (Integer) heightField.get(r);
System.out.println('Height: ' + heightValue.toString());
} catch (NoSuchFieldException e) {
System.out.println(e);
} catch (SecurityException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
}
}
}"

0 Comments:

Post a Comment

<< Home