How to call private method from another class in Java with help of Reflection API?

We can call the private method of a class from another class in Java (which are defined using the private access modifier in Java).
We can do this by changing the runtime behavior of the class by using some predefined methods of Java. For accessing private method of different class we will use Reflection API.
To call the private method, we will use following methods of Java.lang.class and Java.lang.reflect.Method
- Method[] getDeclaredMethods(): This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
- setAccessible(): Set the accessible flag for this object to the indicated boolean value. A value of true indicates that the reflected object should suppress Java language access checking when it is used. A value of false indicates that the reflected object should enforce Java language access checks.
- invoke():It invokes the underlying method represented by this Method object, on the specified object with the specified parameters.
- Example 1: When the name of private function is known already.
// Java program to call// private method of a// class from another classÂÂimportJava.lang.reflect.Method;ÂÂ// The class containing// a private method and// a public methodclassCheck {   Â// Private method   Âprivatevoidprivate_Method()   Â{       ÂSystem.out.println("Private Method "                          Â+"called from outside");   Â}   Â// Public method   ÂpublicvoidprintData()   Â{       ÂSystem.out.println("Public Method");   Â}}ÂÂ// Driver codeclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsException   Â{       ÂCheck c =newCheck();       Â// Using getDeclareMethod() method       ÂMethod m = Check.class                      Â.getDeclaredMethod("private_Method");       Â// Using setAccessible() method       Âm.setAccessible(true);       Â// Using invoke() method       Âm.invoke(c);   Â}} - Example 2: When the name of private function is not known but class name is known.
// Java program to call private method// of a class from another classÂÂimportJava.lang.reflect.Method;ÂÂ// The class contains a private methodclassCheck {   Â// Private method   ÂprivatevoidDemo_private_Method()   Â{       ÂSystem.out.println("Private Method "                          Â+"called from outside");   Â}   Â// Public method   ÂpublicvoidprintData()   Â{       ÂSystem.out.println("Public Method");   Â}}ÂÂ// Driver codeclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsException   Â{       ÂCheck c =newCheck();       ÂMethod m;       Â// Using getDeclareMethod() method       ÂMethod method[]           Â= Check.class.getDeclaredMethods();       Âfor(inti =0; i < method.length; i++) {           ÂString meth               Â=newString(method[i].toString());           Âif(meth.startsWith("private")) {               ÂString s = method[i].toString();               Âinta = s.indexOf(".");               Âintb = s.indexOf("(");               Â// Method name retrieved               ÂString method_name = s.substring(a +1, b);               Â// Print method name               ÂSystem.out.println("Method Name = "                                  Â+ method_name);               Â// Using getDeclareMethod() method               Âm = Check.class.getDeclaredMethod(method_name);               Â// Using setAccessible() method               Âm.setAccessible(true);               Â// Using invoke() method               Âm.invoke(c);           Â}       Â}   Â}}
Below programs demonstrates calling of private method in Java:




