VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-call-private-method-from-another-class-in-java-with-help-of-reflection-api/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 12 Jul, 2025

We can call the private method of a class from another class in Java (which is 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 methods of different classes we will use Reflection API. To call the private method, we will use the 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.
The below programs demonstrate the calling of private methods in Java: Example 1: When the name of the private function is known already. Output:👁 Output of the Program 1
Example 2: When the name of private function is not known but class name is known.
Comment