Skip to content

Instantly share code, notes, and snippets.

@rahulkmr
Created February 7, 2010 07:30
Show Gist options
  • Save rahulkmr/297280 to your computer and use it in GitHub Desktop.
Save rahulkmr/297280 to your computer and use it in GitHub Desktop.
import java.lang.reflect.*;
class Foo
{
private int i = 10;
public int getI() { return i; }
}
class PrivateTest
{
public static void main(String[] args)
throws Exception
{
Foo bar = new Foo();
Field[] f =
bar.getClass().getDeclaredFields();
for (Field test : f) {
/* The following operation depends on
the installed security manager */
test.setAccessible(true);
/* Changing i from outside the class */
test.setInt(bar, 5);
}
System.out.println(bar.getI());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment