SoFunction
Updated on 2025-05-11

A detailed explanation of reflection API in Java

1. Call the parameterless constructor to get the object return of a class

public static void main(String[] args) throws Exception {
    // The first step of reflection is to get the Class object first    Class c = ;
    // Positioning parameterless constructor object    Constructor constructor = ();
    // brute force to open access rights to the private constructor (if it is a private constructor, private can be used to open it with setAccessible)    (true);
    //Return by initializing the object with no parameter constructor    Person person = (Person) ();
    (person);
}

2. Call the parameter constructor to get the object of a class return

public static void main(String[] args) throws Exception {
    // The first step of reflection is to get the Class object first    Class c = ;
    // Positioning parameterless constructor object    Constructor constructor = (, , );
    // brute force to open access rights to the private constructor (if it is a private constructor, private can be used to open it with setAccessible)    // (true);
    //Return by initializing the object with no parameter constructor    Person person = (Person) ("WFT", 18, "1.88");
    (person);
}

3. Get all member variables

public static void main(String[] args) throws Exception {
    // The first step of reflection is to get the Class object first    Class c = ;
    // Get all declared member variable objects    Field[] fields = ();
    for (Field field : fields) {
        (() + "-->>" + ());
    }
}

4. Get a member variable and assign it a value

public static void main(String[] args) throws Exception {
    // The first step of reflection is to get the Class object first    Class c = ;
    // Get all declared member variable objects    Field nameField = ("name");
    (() + "--->>" + ());
    Person person = new Person();
    (true); // Because it is a private attribute, use violent reflection!    /**
      * Parameter 1: The assigned object
      * Parameter 2: The value of this member variable
      */
    (person, "Wang Dae Hammer");
    (person);
    /**
      * Get the value of the changed member variable
      */
    String name = (person) + "";
    (name);
}

5. Get all methods

public static void main(String[] args) {
    // The first step of reflection is to get the Class object first    Class c = ;
    // Get all methods    Method[] methods = ();
    // Check it out    for (Method method : methods) {
        // Method name Number of parameters Return value type        (() + "->" + () + "->" + ());
    }
}

6. Get a method and execute it

public static void main(String[] args) throws Exception {
    // The first step of reflection is to get the Class object first    Class c = ;
    // Get the setName method    Method setName = ("setName", );
    // Call method    Person person = new Person();
    Object o = (person, "WFT"); // If the method does not return the value, the return value is null    (o);
    (person);
}

This is the end of this article about a detailed explanation of the reflection API in Java. For more related Java reflection API content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!