1. Overview
In this article, we will implement a basic reflection process from which we could access private variables and methods from another class in Java.
The main goal is to show easy way to access private fields and methods. While using reflection API, there can be different methods with their signatures.
2. A class with private members
First of all we are declaring a class with private variables and methods so that we could have all operations using this class.
class UserClass{
/**
* A Private Variable
* Which are accessible within a class
* */
private int abc = 11;
/**
* A private method
* Which is also accessible within a class
* */
private void msg1(){
System.out.println("Hello private Msg!");
}
private void msg2(String msgText){
System.out.println(msgText);
}
private String msg3(String msgText){
return "[" + msgText + "]";
}
}
3. Access private variables
For accessing private fields, we will implement two examples.
Example 3.1 – Access single variable from a class
// This object will be used for access
UserClass userClassObj = new UserClass();
// Single Field Access
Field f = UserClass.class.getDeclaredField("abc");
// Set flag true for accessing private field
f.setAccessible(true);
System.out.println("Filed Name: "+ f.getName());
System.out.println("Field Type: "+ f.getType());
System.out.println("Field Value: " + f.getInt(userClassObj));
In above code you can see that we are getting declared field from a class because it is user defined and private field within a class. We need to set accessible flag true before accessing private field. When you try to get value from a field then a method need object reference to retrieve value from it.
We also can access all declared fields from another class.
Example 3.2 – Access all private variables
// Multiple Fields access
Field[] fields = UserClass.class.getDeclaredFields();
for(Field field: fields){
field.setAccessible(true);
System.out.println("Name: "+ field.getName());
}
We find array of fields from a class which could be access with a foreach loop.
4. Access private methods
A method can have different signature within a class for which there are different ways to invoke it so in following section we are going to implement each step by step.
In following example there is no return type of method and list of parameters.
Example 4.1 – No return type and no parameter
// Method Return type void and no parameters
Method m1 = UserClass.class.getDeclaredMethod("msg1");
m1.setAccessible(true);
System.out.print("Method Name: " + m1.getName() + " > ");
// Call private variable
m1.invoke(userClassObj);
Get a declared method with a method name which can be invoke if you set a accessible flag true.
While getting declared method we set the parameter type class.
Example 4.2 – void return type and String parameter
// Method Return type void and Send String parameter
Method m2 = UserClass.class.getDeclaredMethod("msg2", String.class);
m2.setAccessible(true);
System.out.print("Method Name: " + m2.getName() + " > ");
// Call private variable
m2.invoke(userClassObj, "Hello Text");
You can see on line number 2 that we are getting declared method and telling it method name and parameter class type.
Now we learnt in our previous example for sending parameter but return type. Here in this example we have both return type and parameter in a method.
Example 4.3 – String return type and String parameter
// Method Return type void and Send String parameter
Method m3 = UserClass.class.getDeclaredMethod("msg3", String.class);
m3.setAccessible(true);
System.out.print("Method Name: " + m3.getName() + " > ");
// Call private variable which returns Object
Object object = m3.invoke(userClassObj, "Hello Text");
// Cast Object to String
String msg = String.valueOf(object);
System.out.print(msg);
When we invoke a method it return object data type always, In above code you can see that we have made casting from object to string data type.
5. Complete Program
After merging all above examples into single program like variable and methods examples.
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class PrivateAcessModifiers {
public static void main(String[] args) throws Exception{
// This object will be used for access
UserClass userClassObj = new UserClass();
// Single Field Access
Field f = UserClass.class.getDeclaredField("abc");
// Set flag true for accessing private field
f.setAccessible(true);
System.out.println("Filed Name: "+ f.getName());
System.out.println("Field Type: "+ f.getType());
System.out.println("Field Value: " + f.getInt(userClassObj));
// Multiple Fields access
Field[] fields = UserClass.class.getDeclaredFields();
for(Field field: fields){
field.setAccessible(true);
System.out.println("Name: "+ field.getName());
}
// Method Return type void and no parameters
Method m1 = UserClass.class.getDeclaredMethod("msg1");
m1.setAccessible(true);
System.out.print("Method Name: " + m1.getName() + " > ");
// Call private variable
m1.invoke(userClassObj);
// Method Return type void and Send String parameter
Method m2 = UserClass.class.getDeclaredMethod("msg2", String.class);
m2.setAccessible(true);
System.out.print("Method Name: " + m2.getName() + " > ");
// Call private variable
m2.invoke(userClassObj, "Hello Text");
// Method Return type String and Send String parameter
Method m3 = UserClass.class.getDeclaredMethod("msg3", String.class);
m3.setAccessible(true);
System.out.print("Method Name: " + m3.getName() + " > ");
// Call private variable which returns Object
Object object = m3.invoke(userClassObj, "Hello Text");
// Cast Object to String
String msg = String.valueOf(object);
System.out.print(msg);
}
}
class UserClass{
/**
* A Private Variable
* Which are accessible within a class
* */
private int abc = 11;
/**
* A private method
* Which is also accessible within a class
* */
private void msg1(){
System.out.println("Hello private Msg!");
}
private void msg2(String msgText){
System.out.println(msgText);
}
private String msg3(String msgText){
return "[" + msgText + "]";
}
}
5. Conclusion
We have learnt all possible and easy ways to access private members of another class in java.
We also implemented code to invoke method with different method signatures like a method with void return type and no parameter, a method with string return type and string input parameter.
Wow amazing work done, It solves my problem Thanks buddy 🙂