Zheng
Links: https://www.zhihu.com/question/24304289/answer/147529485
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
First we understand the JVM, what is the Jvm,java virtual machine, Java can cross the platform because of this thing, you can understand as a process, program, but his role is to run your code. Is the Java memory model, we focus on the point, a method area, a stack, a heap, beginners when the teacher does not go into the words only to tell you the Java memory is divided into heaps and stacks, easy to understand point!
If you write a piece of code: Object O=new object ();
Run it Up!
First the JVM will start, Your code is compiled into a. class file and then loaded into the JVM's memory by the ClassLoader, and your class object is loaded into the method area, creating the class object of the object class into the heap, note that this is not a new object, but a class object, each class has only one class object as the method area class The interface of the data structure. Before the JVM creates the object, it checks to see if the class is loaded, finds the class object, and, if it is loaded, allocates memory for your object and initializes the code: new Object ().
The process above is that you write your own code to the JVM to run, run over, the JVM shuts down, and your program stops. (Note: When loading a class, all the information in the method area is added, and then an instance of class is formed, representing the loaded class.) All the information in the method area can be reflected by this class object. )
Why are you talking about this? Because to understand reflection you must know what scenario it is used under.
The main thought of the above program object is his new, program equivalent to write dead to the JVM to run. If a server suddenly encountered a request oh to use a class, oops but not loaded into the JVM, is not to stop to write a piece of code, new, OH Start the server, (Brain stump)!
What is reflection? When our program is running, we need to load some classes dynamically, these classes may not be loaded into the JVM before, for example, the bottom of our project is sometimes with 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! Of course, this is just a reflection of the application, there are other functions, but this example can be better understood!
Java Reflection (ii)