...
Code Block | ||
---|---|---|
| ||
import java.lang.reflect.Field;
public class ReflectionExample {
public static void makeAccessible(String fieldName) {
C c = new C();
try {
Field f = c.getClass().getDeclaredField(fieldName);
System.out.println(f.isAccessible());
f.setAccessible(true);
System.out.println(f.isAccessible());
System.out.println(f.getInt(c));
}
catch(NoSuchFieldException nsfa){}
catch(IllegalAccessException iae) {}
}
}
class C {
private int i = 10;
}
|
...