-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
35 lines (28 loc) · 1.08 KB
/
Copy pathMain.java
File metadata and controls
35 lines (28 loc) · 1.08 KB
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
package aReflection;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Main {
public static void main(String[] args) throws Exception {
Student s = new Student();
Class cls = s.getClass();
Field f = cls.getField("name");
printFieldInfo(f);
Field f1 = cls.getDeclaredField("address");
f1.setAccessible(true);
printFieldInfo(f1);
System.out.println(f.get(s));
f1.set(s, "Shanghai");
f1.set(null, 123);
System.out.println(s.hello());
}
private static void printFieldInfo(Field f) {
System.out.println("field name: " + f.getName());
System.out.println("field type: " + f.getType());
System.out.println("field modifier: " + f.getModifiers());
System.out.println("is public? : " + Modifier.isPublic(f.getModifiers()));
System.out.println("is private? : " + Modifier.isPrivate(f.getModifiers()));
System.out.println("is protected? : " + Modifier.isProtected(f.getModifiers()));
System.out.println("is static? : " + Modifier.isStatic(f.getModifiers()));
System.out.println("is final? : " + Modifier.isFinal(f.getModifiers()));
}
}