How to call private method from another class in Java with help of Reflection API? Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Java // Java program to call // private method of a // class from another class import Java.lang.reflect.Method; // The class containing // a private method and // a public method class Check { // Private method private void private_Method() { System.out.println("Private Method " + "called from outside"); } // Public method public void printData() { System.out.println("Public Method"); } } // Driver code class GFG { public static void main(String[] args) throws Exception { Check c = new Check(); // Using getDeclareMethod() method Method m = Check.class .getDeclaredMethod("private_Method"); // Using setAccessible() method m.setAccessible(true); // Using invoke() method m.invoke(c); } } Output: Example 2: When the name of private function is not known but class name is known. Java // Java program to call private method // of a class from another class import Java.lang.reflect.Method; // The class contains a private method class Check { // Private method private void Demo_private_Method() { System.out.println("Private Method " + "called from outside"); } // Public method public void printData() { System.out.println("Public Method"); } } // Driver code class GFG { public static void main(String[] args) throws Exception { Check c = new Check(); Method m; // Using getDeclareMethod() method Method method[] = Check.class.getDeclaredMethods(); for (int i = 0; i < method.length; i++) { String meth = new String(method[i].toString()); if (meth.startsWith("private")) { String s = method[i].toString(); int a = s.indexOf("."); int b = 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); } } } } Output: Comment More infoAdvertise with us Next Article Private and Final Methods in Java R Rajnis09 Follow Improve Article Tags : Java access modifiers Java-Functions java-lang-reflect-package Practice Tags : Java Similar Reads How to Invoke Method by Name in Java Dynamically Using Reflection? Java Reflection API provides us information about a Class to which the Object belongs to including the methods in this class. Using these Reflection API we would be able to get invoking pointer for a method in a class with its name. There are two functions used for this purpose: Invoking method with 4 min read More restrictive access to a derived class method in Java As private, protected and public (access modifiers) affect the accessibility and scope of the field. So, the method cannot be private which are called from outside the class. In Program 1 : We create the object for Derived class and call foo function, but this foo function is private i.e., its scop 2 min read Private vs Protected vs Final Access Modifier in Java Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi 5 min read Private and Final Methods in Java Methods in Java play an important role in making the code more readable, support code reusability, and defining the behaviour of the objects. And we can also restrict the methods based on requirements using the keywords and modifiers such as final and private. These two have different, distinct purp 5 min read How to Call a Method in Java? In Java, calling a method helps us to reuse code and helps everything be organized. Java methods are just a block of code that does a specific task and gives us the result back. In this article, we are going to learn how to call different types of methods in Java with simple examples.What is a Metho 4 min read How to Call a Method in Java? In Java, calling a method helps us to reuse code and helps everything be organized. Java methods are just a block of code that does a specific task and gives us the result back. In this article, we are going to learn how to call different types of methods in Java with simple examples.What is a Metho 4 min read Like