Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

import java.lang.reflect.Method; 

class Check { 
    private void privateMethod() { 
        System.out.println("A private method is called from outside");
    } 
    
    private void privateMethodWithParam(String fname, long n) { 
        System.out.println("Hello " + fname + " " + n + ", a private Method is called from outside.");
    }
    
    public void printData() { 
        System.out.println("Public Method"); 
    } 
}
com.twosigma.beaker.javash.bkr95ac1ac9.Check

Invoke a private method without parameters.

import java.lang.reflect.Method; 

Check check = new Check(); 
// get the private method 
Method method = Check.class.getDeclaredMethod("privateMethod"); 
// make it accessible
method.setAccessible(true); 
// invoke the method
method.invoke(check); 
A private method is called from outside
null

Invoke a private method with parameters.

import java.lang.reflect.Method; 

Check check = new Check(); 
// get the private method 
Method method = Check.class.getDeclaredMethod("privateMethodWithParam", String.class, long.class); 
// make it accessible
method.setAccessible(true); 
// invoke the method
method.invoke(check, "Ben", 2); 
Hello Ben 2, a private Method is called from outside.
null