Field: setAccessible(boolean flag)
import java.lang.reflect.Field;
public class Main {
public static void initIntFields(final Object obj) {
try {
Field[] fields = obj.getClass().getDeclaredFields();
for (int idx = 0; idx < fields.length; idx++) {
if (fields[idx].getType() == int.class) {
fields[idx].setAccessible(true);
fields[idx].setInt(obj, 0);
}
}
} catch (final IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
public static final void main(final String[] args) {
Integer value = new Integer("123");
System.out.println("Before: " + value);
initIntFields(value);
System.out.println("After: " + value);
}
}
Related examples in the same category