Java

Sometimes you are writing a test class and want to check that the class you are testing is being changed the way you expect.  When a class has private menbers that you don't want to open up to anyone,  you can use reflection to see them.  This is a simple example.

import java.lang.reflect.Field;

class ExampleClass {
private String cantTouchThis = "Secret Values"; // private field
}

public class PrivateMemberAccessTest {
public static void main(String[] args) throws Exception {
ExampleClass example = new ExampleClass();
Class c = example.getClass();

// get the reflected object
Field field = c.getDeclaredField("cantTouchThis");
        // set accessible true 
field.setAccessible(true);
System.out.println("Value of
cantTouchThis: " + field.get(example));

// modify the member varaible
field.set(keyPair, "Yes I can");
System.out.println("Value of
cantTouchThis: " + field.get(example)); }
}