We can call the private method of a class from another class in Java (which are 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 method of different class we will use Reflection API.
To call the private method, we will use 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.
- Example 1: When the name of private function is known already.
// 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);
}
}
- Example 2: When the name of private function is not known but class name is known.
// 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);
}
}
}
}
Below programs demonstrates calling of private method in Java: