According to the basics of Java, its not possible to access method that are marked as private outside the class.

But thats not the case. We can access it outside the class by using “java.lang.reflect” and the “setAccessible” method of the “Method” class.

Here is a code snippet on how to use it.

import java.lang.reflect.*;

class A {
private void f() {
System.out.println(“Running A.f()”);

public class Main {
public static void main(String[] args) throws Exception {
Class a = new A().getClass();
Method m = a.getDeclaredMethod(“f”, null);
m.invoke(new A(), null);