Annotations and Reflection
Field Access
Reflection can look up a field and read its value from an object.
Field Access
FieldAccess.java
import java.lang.reflect.Field;
public class FieldAccess {
static class Box {
public int value;
Box(int value) {
this.value = value;
}
}
public static void main(String[] args) throws Exception {
int start = ;
Box box = new Box(start);
Field field = Box.class.getDeclaredField("value");
int loaded = (int) field.get(box);
System.out.println("loaded=" + loaded);
}
}
import java.lang.reflect.Field;
public class FieldAccess {
static class Box {
public int value;
Box(int value) {
this.value = value;
}
}
public static void main(String[] args) throws Exception {
int start = ;
Box box = new Box(start);
Field field = Box.class.getDeclaredField("value");
int loaded = (int) field.get(box);
System.out.println("loaded=" + loaded);
}
}
import java.lang.reflect.Field;
public class FieldAccess {
static class Box {
public int value;
Box(int value) {
this.value = value;
}
}
public static void main(String[] args) throws Exception {
int start = ;
Box box = new Box(start);
Field field = Box.class.getDeclaredField("value");
int loaded = (int) field.get(box);
System.out.println("loaded=" + loaded);
}
}
field metadata
A Field object describes one field declared on a class.
reflected read
Calling get on a Field reads that field from a specific object.