The difference between getdeclaredfields () and GetFields () in Java
Getdeclaredfields () returns all fields in class, including private fields. Example:
Java code
- Package Com.test.bean;
- Import Java.sql.Timestamp;
- Public class Person2 {
- private int id;
- private int age;
- private String PersonName;
- private Timestamp birthdate;
- Public String identitify;
- protected String address;
- String phone;
- }
- @Test
- public void Test_getdeclaredfields () {
- Field[]fields=person2. Class.getdeclaredfields ();
- For (int i = 0; i < fields.length; i++) {
- Field field = Fields[i];
- System.out.println (Field.getname ());
- }
- }
Operation Result:
Id
Age
PersonName
Birthdate
Identitify
Address
Phone
GetFields returns only public fields, that is, fields that have the publicly adornment. Example:
Java code
- @Test
- public void Test_getdeclaredfields () {
- Field[]fields=person2. Class.getfields ();
- For (int i = 0; i < fields.length; i++) {
- Field field = Fields[i];
- System.out.println (Field.getname ());
- }
- }
The results of the operation are as follows:
Identitify
Summarize:
(1) Getdeclaredfields () returns all fields in class, including private fields;
(2) GetFields returns only public fields, that is, fields that have publicly decorated
The difference between getdeclaredfields () and GetFields () in Java