You can call the private method from outside the class by changing the runtime behavior of the class.
With the help of java.lang.Class class and java.lang.reflect.Method class, we can call a private method from any other class.
Required methods of Method class
1) public void setAccessible(boolean status) throws SecurityException sets the accessibility of the method.
2) public Object invoke(Object method, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException is used to invoke the method.
Required method of Class class
1) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws
NoSuchMethodException,SecurityException: returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
Example of calling private method from another class
Let's see the simple example to call private method from another class.
File: A.java
private void message(){System.out.println("hello java"); }
}
File: MethodCall.java
import java.lang.reflect.Method;
public class MethodCall{
public static void main(String[] args)throws Exception{
Class c = Class.forName("A");
Object o= c.newInstance();
Method m =c.getDeclaredMethod("message", null);
m.setAccessible(true);
m.invoke(o, null);
}
}
Output:
hello java
Another example to call parameterized private method from another class
Let's see the example to call parameterized private method from another class
File: A.java
private void cube(int n){System.out.println(n*n*n);}
}
File: M.java
import java.lang.reflect.*;
class M{
public static void main(String args[])throws Exception{
Class c=A.class;
Object obj=c.newInstance();
Method m=c.getDeclaredMethod("cube",new Class[]{int.class});
m.setAccessible(true);
m.invoke(obj,4);
}}
Output:
64
Accessing Private Constructors of a class
We know that constructors of a class are a special kind of method this is used to instantiate the class. To access the private constructor, we use the method getDeclaredConstructor(). The getDeclaredConstructor() is used to access a parameterless as well as a parametrized constructor of a class. The following example shows the same.
FileName: PvtConstructorDemo.java
// important import statements
import java.lang.reflect.Modifier;
import java.lang.reflect.InvocationTargetException;
class Vehicle
{
// private fields of the class Vehicle
private Integer vId;
private String vName;
// parameterless constructor
private Vehicle()
{
}
// parameterized constructor
private Vehicle(Integer vId, String vName)
{
this.vId = vId;
this.vName = vName;
}
// setter methods of the class Vehicle
public void setVehicleId(Integer vId)
{
this.vId = vId;
}
public void setVehicleName(String vName)
{
this.vName = vName;
}
// getter methods of the class Vehicle
public Integer getVehicleId()
{
return vId;
}
public String getVehicleName()
{
return vName;
}
}
{
// the createObj() method is used to create an object of
// the Vehicle class using the parameterless constructor.
public void craeteObj(int vId, String vName) throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException
{
// using the parametereless contructor
Constructor<Vehicle> constt = Vehicle.class.getDeclaredConstructor();
constt.setAccessible(true);
Object obj = constt.newInstance();
if (obj instanceof Vehicle)
{
Vehicle v = (Vehicle)obj;
v.setVehicleId(vId);
v.setVehicleName(vName);
System.out.println("Vehicle Id: " + v.getVehicleId());
System.out.println("Vehicle Name: " + v.getVehicleName());
}
}
// the craeteObjByConstructorName() method is used to create an object
// of the Vehicle class using the parameterized constructor.
public void craeteObjByConstructorName(int vId, String vName) throws NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
// using the parameterized contructor
Constructor<Vehicle> constt = Vehicle.class.getDeclaredConstructor(Integer.class, String.class);
if (Modifier.isPrivate(constt.getModifiers()))
{
constt.setAccessible(true);
Object obj = constt.newInstance(vId, vName);
if(obj instanceof Vehicle)
{
Vehicle v = (Vehicle)obj;
System.out.println("Vehicle Id: " + v.getVehicleId());
System.out.println("Vehicle Name: " + v.getVehicleName());
}
}
}
// exception
// main method
public static void main(String argvs[]) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException
{
// creating an object of the class PvtConstructorDemo
PvtConstructorDemo ob = new PvtConstructorDemo();
ob.craeteObj(20, "Indica");
System.out.println(" -------------------------- ");
ob.craeteObjByConstructorName(30, "Alto");
}
}
Output:
Vehicle Id: 20
Vehicle Name: Indica
--------------------------
Vehicle Id: 30
Vehicle Name: Alto