Algorithm
-
Import necessary packages:
- Import
java.lang.reflect.Field
for accessing fields. - Import
java.lang.reflect.Method
for accessing methods.
- Import
-
Create an instance of the class with private members:
- Instantiate the class for which you want to access private members.
-
Access private fields:
- Use the
getDeclaredField
method of theClass
class to obtain theField
object for the private field. - Set the field accessible by calling
setAccessible(true)
on the obtainedField
object. - Use the
get
method on the field to retrieve its value.
- Use the
-
Access private methods:
- Use the
getDeclaredMethod
method of theClass
class to obtain theMethod
object for the private method. - Set the method accessible by calling
setAccessible(true)
on the obtainedMethod
object. - Use the
invoke
method on the method to execute it.
- Use the
-
Handle exceptions:
- Handle
NoSuchFieldException
,IllegalAccessException
,NoSuchMethodException
, and other relevant exceptions that may be thrown during reflection.
- Handle
Code Examples
#1 Code Example- Access private fields using getter and setter methods
Code -
Java Programming
class Test {
// private variables
private int age;
private String name;
// initialize age
public void setAge(int age) {
this.age = age;
}
// initialize name
public void setName(String name) {
this.name = name;
}
// access age
public int getAge() {
return this.age;
}
// access name
public String getName() {
return this.name;
}
}
class Main {
public static void main(String[] args) {
// create an object of Test
Test test = new Test();
// set value of private variables
test.setAge(24);
test.setName("Devsenv");
// get value of private variables
System.out.println("Age: " + test.getAge());
System.out.println("Name: " + test.getName());
}
}
Copy The Code &
Try With Live Editor
Output
Age: 24
Name: Devsenv
#2 Code Example- Access the private field and method using Reflection
Code -
Java Programming
import java.lang.reflect.*;
class Test {
// private variables
private String name;
// private method
private void display() {
System.out.println("The name is " + name);
}
}
class Main {
public static void main(String[] args) {
try {
// create an object of Test
Test test = new Test();
// create an object of the class named Class
Class obj = test.getClass();
// access the private variable
Field field = obj.getDeclaredField("name");
// make private field accessible
field.setAccessible(true);
// set value of field
field.set(test, "Devsenv");
// get value of field
// and convert it in string
String value = (String)field.get(test);
System.out.println("Name: " + value);
// access the private method
Method[] methods = obj.getDeclaredMethods();
System.out.println("Method Name: " + methods[0].getName());
int modifier = methods[0].getModifiers();
System.out.println("Access Modifier: " + Modifier.toString(modifier));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Copy The Code &
Try With Live Editor
Output
Name: Devsenv
Method Name: display
Access Modifier: private
Method Name: display
Access Modifier: private
Demonstration
Java Programing Example to Access private members of a class-DevsEnv