Java reflection mechanism is in the running state, for any class , can know all the properties and methods of this class, for any one object , can call any of its methods and properties This dynamic acquisition of information and the ability to dynamically invoke the object's methods is called the reflection mechanism of the Java language.
The Java reflection mechanism mainly provides the following purposes:
- Determine the class to which any object belongs at run time
- To construct an object of any class at run time
- Determine the member variables and methods that any one class has at run time
- Methods to invoke any object at run time
- * Using the reflection mechanism of Java, it is generally necessary to follow three steps:
-
- Get the class object you want to manipulate
- The class object obtained by the first step gets the method or property name of the operation
- The method or property obtained in the second step of the operation
When Java runs, a class, regardless of how many objects are generated, will correspond to the same class object, which represents the classes and interfaces in the program that are running. There are three ways to get the class object for the action classes:
- Call the static method of class forname;
- Use the class's. class syntax, such as:class<?> cls = String.class;
- Call the GetClass method of the object, such as: String str = "ABC";class<?> CLS = str. getclass ();
A brief analysis of Java reflection mechanism