Java's reflection mechanism is one of Java's features, and the reflection mechanism is the basis for building framework technology. Flexibly mastering the Java reflection mechanism will be of great help to everyone in learning framework technology in the future.
#What is reflection in Java?
Reflection is one of the features of Java and is a mechanism for indirectly operating target objects.
The JAVA reflection mechanism is that in the running state, for any entity class, all properties and methods of this class can be known; for any object, any method and property of it can be called; this kind of dynamic The function of obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.
What does Java reflection do?
Through reflection, the program code can access the internal information of the class loaded into the JVM
● Get the member variable information of the loaded class
● Get the loaded class Class method
● Obtain the constructor information of the loaded class
Application occasions of reflection:
There is no way to know the object or What classes a class may belong to, the program relies only on runtime information to discover the true information about the object and class.
Disadvantages of reflection
Performance issues
Using reflection is basically an interpretation operation, which is much slower when used for field and method access. to direct code. Therefore, the Java reflection mechanism is mainly used in system frameworks that require high flexibility and scalability, and is not recommended for ordinary programs.
Using reflection will obscure the internal logic of the program
Programmers hope to see the logic of the program in the source code. Reflection and other technologies that bypass the source code will cause maintenance problems. Reflected code is more complex than the corresponding direct code.
Recommended learning: Java video tutorial
The above is the detailed content of What is the role of java reflection?. For more information, please follow other related articles on the PHP Chinese website!