In Java, reflection is loaded into the runtime dynamically. One great advantage is that you can save a lot of resources. Why, first of all, let's say that JVM,JVM is a Java virtual machine, and Java supports cross-platform because of the existence of a Java virtual machine. Program you have the following statement object O=new object (); When running, Java's virtual opportunity starts first. Compile your Java file into a. class file. Load into the memory of your JVM. Your class object loads into the method area, which generates classes of type objects (that is, objects of the class class) to be loaded into the heap as interfaces to the data structures of the method area classes. When the JVM creates the object, it will see if your class is loaded, and the object that generated your class is loaded. However, in large projects, many of them may not be available for the time being, so it is not necessary to generate objects for each class. And the reflection mechanism is the solution to this problem.
For example, the bottom of our project is sometimes MySQL, sometimes with Oracle, we need to dynamically load the driver class according to the actual situation, this time the reflection is useful, assuming Com.java.dbtest.myqlconnection,com.java.dbtest.oracleconnection These two classes we want to use, this time our program is written more dynamic, through the class TC = Class.forName ("Com.java.dbtest.TestConnection"), which allows the JVM to find and load this class in the server through the class's full class name, and if it is Oracle, the passed-in parameter becomes another. At this point you can see the benefits of reflection, the dynamics of the characteristics of the Java is reflected! For example, if you touch spring, you will find that when you configure a variety of beans, it is configured as a configuration file, which beans you need to match, and the spring container will dynamically load according to your needs, and your program will be able to run robustly.
Reflection gets to the class through the object, Employee Employee=new employee (); Employee.getclass (); GetClass is a method in the object class.
Note that the class file is the source of the reflection mechanism. Just find the class type object for the classes you are using. You can find the way to your class. You can call the method of this class.
There are three ways to get objects of class classes.
Such as
Learn from the following big-guy articles.
Old Urchin Botong Tong
Links: https://www.zhihu.com/question/24304289/answer/147529485
Source: Know
The benefits of the reflection mechanism of Java