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 classimportJava.lang.reflect.Method;// The class containing// a private method and// a public methodclassCheck {// Private methodprivatevoidprivate_Method(){System.out.println("Private Method "+"called from outside");}// Public methodpublicvoidprintData(){System.out.println("Public Method");}}// Driver codeclassGFG {publicstaticvoidmain(String[] args)throwsException{Check c =newCheck();// Using getDeclareMethod() methodMethod m = Check.class.getDeclaredMethod("private_Method");// Using setAccessible() methodm.setAccessible(true);// Using invoke() methodm.invoke(c);}}chevron_rightfilter_none - 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 classimportJava.lang.reflect.Method;// The class contains a private methodclassCheck {// Private methodprivatevoidDemo_private_Method(){System.out.println("Private Method "+"called from outside");}// Public methodpublicvoidprintData(){System.out.println("Public Method");}}// Driver codeclassGFG {publicstaticvoidmain(String[] args)throwsException{Check c =newCheck();Method m;// Using getDeclareMethod() methodMethod 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 retrievedString method_name = s.substring(a +1, b);// Print method nameSystem.out.println("Method Name = "+ method_name);// Using getDeclareMethod() methodm = Check.class.getDeclaredMethod(method_name);// Using setAccessible() methodm.setAccessible(true);// Using invoke() methodm.invoke(c);}}}}chevron_rightfilter_none - In Java, Can we call the main() method of a class from another class?
- Reflection Array Class in Java
- JavaFX | Reflection Class
- Reflection in Java
- Can we override private methods in Java?
- Private and final methods in Java
- Private Methods in Java 9 Interfaces
- Private Constructors and Singleton Classes in Java
- Replacing 'public' with 'private' in "main" in Java
- Method Class | getGenericParameterTypes() method in Java
- Method Class | getAnnotatedReturnType() method in Java
- Method Class | toGenericString() method in Java
- Method Class | getParameterAnnotations() method in Java
- Method Class | getModifiers() method in Java
- Method Class | getDeclaredAnnotations() method in Java
Below programs demonstrates calling of private method in Java:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




