What is reflection?
Java reflection means that in the running state, for any class, we can know what methods and attributes this class has. For any object, we can call its methods and properties. We call this function of dynamically obtaining object information and calling object methods a reflection mechanism.
Three ways of reflection
I need to tell you here that the so-called reflection is actually to obtain the bytecode file of the class, that is, the .class file, then we It can be obtained through the Class object.
Method 1:
This method is actually a method of Object. Class inherits Object, so we can use it directly.
Method 2:
Method 3:
It should be noted here that through the entire class Obtaining the Class object by path name will throw an exception. If the class cannot be found based on the class path, this exception will be thrown.
Recommended tutorial:java tutorial
The above is the detailed content of How to understand reflection in java. For more information, please follow other related articles on the PHP Chinese website!