Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
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;
}

...