-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDemo2.java
More file actions
32 lines (28 loc) · 850 Bytes
/
Demo2.java
File metadata and controls
32 lines (28 loc) · 850 Bytes
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
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
class T
{
private int x,y,z;
public int r;
static int p;
}
public class Demo2 {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// TODO Auto-generated method stub
int privateCount = 0;
T obj = new T();
Class cls = obj.getClass(); // Class Meta Data + Data
// obj.r
cls.getDeclaredField("r").set(obj, 1000);
System.out.println("Data is "+cls.getDeclaredField("r").get(obj));
//Class cls = T.class; // Class Meta Data
Field fields [] = cls.getDeclaredFields();
System.out.println(fields.length);
for(Field f : fields){
if(Modifier.isPrivate(f.getModifiers())){
privateCount++;
}
}
System.out.println("No of Private Variables are "+privateCount);
}
}