I have a Dummy
class that has a private method called sayHello
. I want to call sayHello
from outside Dummy
. I think it should be possible with reflection but I get an IllegalAccessException
. Any ideas???

- 122,468
- 25
- 185
- 269

- 3,439
- 3
- 24
- 38
-
14Isn't the idea of private that you can't call it from outside? – PriestVallon Jul 01 '12 at 13:16
-
Yes, it's possible with reflection, but the point of private is to make it harder to call methods from the outside. Maybe it _shouldn't be private_? – Louis Wasserman Jul 01 '12 at 13:18
-
@robert it's in the same program (module) – Hamed Rajabi Varamini Jul 01 '12 at 13:21
-
@HamedRajabi: you mean the class that call the private method and your `Dummy` class is in the same package? If that's the case, you may want to use `package-private` (omitting the modifier). – Genzer Jul 01 '12 at 13:29
-
1@PriestVallon Yes I know I'm not supposed to do this in a real program, I was just wondering!!! – Hamed Rajabi Varamini Jul 01 '12 at 13:30
5 Answers
use setAccessible(true)
on your Method object before using its invoke
method.
import java.lang.reflect.*;
class Dummy{
private void foo(){
System.out.println("hello foo()");
}
}
class Test{
public static void main(String[] args) throws Exception {
Dummy d = new Dummy();
Method m = Dummy.class.getDeclaredMethod("foo");
//m.invoke(d);// throws java.lang.IllegalAccessException
m.setAccessible(true);// Abracadabra
m.invoke(d);// now its OK
}
}
If someone is interested in invoking methods with parameters see How do I invoke a Java method when given the method name as a string?, specifically answer like https://stackoverflow.com/a/30671481.
Just don't forget to add setAccessible(true)
while invoking private
methods.

- 122,468
- 25
- 185
- 269
-
4because `getMethod` only returns public method, you need `getDeclaredMethod` – Pshemo Jul 01 '12 at 13:23
-
1
-
-
@vizzer Main problem/topic in this question is `IllegalAccessException` which means OP and reader most likely already know *general* way to use reflection to call method but are facing problem specifically with `private` methods. We already have many questions and answers about invoking methods with parameters via reflection like https://stackoverflow.com/a/30671481 so I don't feed the need to repeat it here, especially since that topic is broad and may require info about problems like handling primitive type arguments, difference between getDeclaredMethod vs getMethod, etc. – Pshemo Jun 19 '23 at 14:36
-
1@vizzer Anyway I linked question with that subject at the bottom of my answer. Hope that is OK with you. – Pshemo Jun 19 '23 at 14:50
First you gotta get the class, which is pretty straight forward, then get the method by name using getDeclaredMethod
then you need to set the method as accessible by setAccessible
method on the Method
object.
Class<?> clazz = Class.forName("test.Dummy");
Method m = clazz.getDeclaredMethod("sayHello");
m.setAccessible(true);
m.invoke(new Dummy());

- 2,456
- 2
- 15
- 23
method = object.getClass().getDeclaredMethod(methodName);
method.setAccessible(true);
method.invoke(object);

- 2,325
- 1
- 23
- 18
If you want to pass any parameter to private function you can pass it as second, third..... arguments of invoke function. Following is sample code.
Method meth = obj.getClass().getDeclaredMethod("getMyName", String.class);
meth.setAccessible(true);
String name = (String) meth.invoke(obj, "Green Goblin");
Full example you can see Here

- 399
- 3
- 10
Example of accessing private method(with parameter) using java reflection as follows :
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Test
{
private void call(int n) //private method
{
System.out.println("in call() n: "+ n);
}
}
public class Sample
{
public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException
{
Class c=Class.forName("Test"); //specify class name in quotes
Object obj=c.newInstance();
//----Accessing private Method
Method m=c.getDeclaredMethod("call",new Class[]{int.class}); //getting method with parameters
m.setAccessible(true);
m.invoke(obj,7);
}
}

- 7,515
- 3
- 24
- 21