forked from davidjava1991/java17CompleteCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
36 lines (33 loc) · 1.05 KB
/
Copy pathExample2.java
File metadata and controls
36 lines (33 loc) · 1.05 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
36
package com.david.class137;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Example2 {
public static void main(String[] args) {
try {
Class<Student> c = Student.class;
System.out.println("class name = "+c.getName());
Field[] f = c.getDeclaredFields();
Student s = new Student();
for(Field f1 : f ) {
System.out.println("field name = "+f1.getName());
System.out.println("field type = "+f1.getType());
System.out.println("field value = "+f1.getInt(s));
f1.setInt(s, 100);
System.out.println("field value after = "+f1.getInt(s));
}
Method[] m = c.getDeclaredMethods();
for(Method m1 : m) {
System.out.println("Method name "+m1.getName());
System.out.println("invoke");
m1.invoke(s, null);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}