Gets the property structure and its internal structure of the runtime class [Java]

Gets the attribute structure and its internal structure of the runtime class

The internal structure of the attribute structure we are talking about here is the permission modifier of the attribute, the data type of the attribute, the name of the variable, and so on.

There are two ways to get the attribute structure of a runtime class:

  1. getFields();

    • Gets the properties declared as public access rights in the current runtime class and its parent
    • The return value type is: Field [] type
      • This Field is the API we use in java to represent attributes
  2. getDeclaredFields();

    • Gets the properties of all permissions declared in the current runtime class (excluding those declared in the parent class)
    • The return value type is Field[]

If we want to get the specific internal structure of the property structure after we get the property structure of the runtime class, we can get it as follows:

  1. int getModifiers()

    • Get the permission modifier for the property
    • Note: The return value type of this method is int, and different int values represent different permission modifiers
      • 0 is default, 1 is public, 2 is private, 4 is protected, 8 is static, 16 is final, 32 is synchronized, 64 is volatile
      • These constants are defined in the Modifiers class. If we want to output the corresponding permission modifier, we need to convert the return value of our int type to the corresponding permission modifier by calling the toString() static method in the Modifier class.
  2. Class getType();

    • Get the data type of the property
    • Return value type is Class type
      • Here we note that once we get the return value of the Class type, we can output it directly, which will output the class name (but not the class name) of the corresponding runtime class of the Class if we want to output the class name as a whole. At this point we can call the getName() method from this Class type object and return the full class name corresponding to this Class type object
  3. String getName();

    • Get the property name (variable name)
    • Return value type is String

Here's an example of how to get the attribute structure and internal structure of a runtime class

In this example we use a custom class Person, let's first give the custom class Person

package reflex.Get members of runtime classes;

import annotation.type annotation.MyAnnotation;

public class Person {
    public int age;
    protected String sex;
    String name;
    private int tizhong;

    public Person(){


    public Person(int age ,String sex , String name , int tizhong){
        this.age = age; = sex; = name;
        this.tizhong = tizhong;

    public String toString() {
        return "Person{" +
                "age=" + age +
                ", sex='" + sex + '\'' +
                ", name='" + name + '\'' +
                ", tizhong=" + tizhong +

    public void eat() throws Exception{
        System.out.println("Human eating");
        System.out.println("Human eating");

    public String play(int x , int y , String z){
        return z;

    //Note: We don't have a life cycle for this comment, at which point it defaults to "class retention"
    @annotation.Custom Notes.MyAnnotation
    protected void see(){


    private void feel(){


Then here's another example (that is, the test program)

package reflex.Get the attribute structure and internal structure of the runtime class;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class Demo {
    public static void main(String[] args) {
        Gets the property structure of the runtime class, which at this time is the property we declare as public permission in the parent of the Person class and Person class

        At this point we just declared a public permission property in the Person class
        Field [] fields = Person.class.getFields();
        for (Field f:
             fields) {

        At this point we get all the permissions declared in the Person class (including the private method) through the getDeclaredFields() method, but note that this time we won't get all the permissions declared in the Person class parent
        Properties of
        Field [] fields1 = Person.class.getDeclaredFields();
        for (Field f1:
             fields1) {
            Here we get the internal structure of the corresponding attribute structure by calling the method in the Field class
            //1. Access modifiers for properties
            int modifiers = f1.getModifiers();

            //2. Data type to get attributes
            Class clazz = f1.getType();

            //3. Get the property name (variable name)
            String s = f1.getName();

Keywords: Java Back-end

Added by aayatoos on Mon, 28 Feb 2022 19:44:51 +0200