-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathReflectionExample.java
39 lines (32 loc) · 1.46 KB
/
ReflectionExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.lang.reflect.Field;
class MyClass {
private int privateField;
public String publicField;
protected double protectedField;
public MyClass(int privateField, String publicField, double protectedField) {
this.privateField = privateField;
this.publicField = publicField;
this.protectedField = protectedField;
}
public void printFields() {
System.out.println("privateField: " + privateField);
System.out.println("publicField: " + publicField);
System.out.println("protectedField: " + protectedField);
}
}
public class ReflectionExample {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
MyClass myObject = new MyClass(42, "Hello, Reflection!", 3.14);
// Accessing publicField using reflection
Field publicField = myObject.getClass().getField("publicField");
String fieldValue = (String) publicField.get(myObject);
System.out.println("Accessing publicField using reflection: " + fieldValue);
// Modifying privateField using reflection
Field privateField = myObject.getClass().getDeclaredField("privateField");
privateField.setAccessible(true); // Allow access to private field
privateField.setInt(myObject, 123);
// Printing the fields using the printFields method
System.out.println("Modifying privateField using reflection:");
myObject.printFields();
}
}