Invoking private method and accessing private fields using Java reflection


In general we can not access private field and method via object in Java. But using reflection we can do in normal standalone application. But In applications like applet or android application it requires special permission to access private fields and methods.

Here is  sample private class with one private field and three methods(return value, with parameter, without parameter) to explain how you can actually do this.

class MYClass{
    private String name;
    public MYClass(String name)
    {
        this.name = name;
    }
    private void printName(String value)
    {
        System.out.println("Name:"+value);
    }
    private void printName()
    {
        System.out.println("Name:"+name);
    }
    private String getName()
    {
        return name;
    }
}

Invoking private method

To access private method Java reflection class Class provides two methods Class.getDeclaredMethod(String name, Class[] parameterTypes) and Class.getDeclaredMethods() by using anyone of these two you can invoke private method(s). This class also provides two methods Class.getMethod(String name, Class[] parameterTypes) and Class.getMethods() but these methods can be use to invoke public methods only as first one will return only matched public method and second will return all public methods.

Refer following example to use Class.getDeclaredMethod(String name, Class[] parameterTypes) for invoking method if you know name and signature of method. While Class.getDeclaredMethods()  is used to list all declared methods in class and by using some logic you can invoke particular method.

MYClass privateObject = new MYClass("My Name is Anupama");
//Without parameter
Method privateMethod = MYClass.class.getDeclaredMethod("printName", null);
privateMethod.setAccessible(true);
privateMethod.invoke(privateObject,null);
//Assign all parameter type in Class<?> Array
privateMethod = MYClass.class.getDeclaredMethod("printName", new Class<?>[]{String.class});
privateMethod.setAccessible(true);
privateMethod.invoke(privateObject, "My Name is Anupama From private method param");
//With return value
privateMethod = MYClass.class.getDeclaredMethod("getName", null);
privateMethod.setAccessible(true);
String res =(String)privateMethod.invoke(privateObject, null);
System.out.println("With return value: "+res);

Accessing private field

To access private fields Java reflection class Class provides two methods Class.getDeclaredField(String name) and Class.getDeclaredFields() by using anyone of these two you can invoke private fields(s). This class also provides two methods Class.getField(String name, Class[] parameterTypes) and Class.getFieldss() but these methods can be use to invoke public fields only as first one will return only matched public field and second will return all public fields.

Refer following example to use Class.getDeclaredField(String name) for accessing field if you know name and signature of method. While Class.getDeclaredFields()  is used to list all declared fields in class and by using some logic you can access particular fields.

Field privateField = MYClass.class.getDeclaredField("name");

privateField.setAccessible(true);

String fieldValue = (String) privateField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);