See the old urchin Zhou Botong's reply and in-depth analysis of the reflection of the Java reflection of the explanation, finally the concept of reflection has a new understanding. No longer the lily, reproduced the essence of the section.
First, why use the reflection?
If you write a piece of code: Object O=new object ();
Run it Up!
First the JVM will start, your code will be compiled into a. class file, then loaded into the JVM's memory by the ClassLoader, your class object is loaded into the method area, the object class object is created into the heap, note that this is not the new objects, Instead, the type object of the class ,
Each class has only one class object, which is the interface for the data structure of the method area class. 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 ().
To understand what the reflection is used in.
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)!
old urchin Botong Tong
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. second, what is reflection? Reflection (Reflection) is one of the characteristics of a Java programming language that allows a running Java program to obtain its own information and to manipulate the internal properties of a class or object. with reflection, we can get information about the members and members of each type in a program or assembly at run time. The type of the object in the program is determined at compile time, and the Java reflection mechanism can dynamically create the object and invoke its properties, so that the type of the object is unknown at compile time. so we can create objects directly from the reflection mechanism, even if the type of the object is unknown at compile time. third, the role of reflection?
The Java Reflection Framework mainly provides the following features:
- 1. Determine the class to which any object belongs at run time;
- 2. Constructs an object of any class at run time;
- 3. At run time, judge the member variables and methods that any one class has (through reflection can even call the private method);
- 4. Methods for invoking any object at run time Focus: is run sometimes not compile time
Iv. Basic Application of Reflection
1, Get Class object 2, determine whether it is an instance of a Class 3, create an instance 4, Get method 5, get the constructor information 6, get the class member variable (field) information 7, call method
Http://www.sczyh30.com/posts/Java/java-reflection-1/#%E4%B8%80%E3%80%81%E5%9B%9E%E9%A1%BE%EF%BC%9A%E4%BB%80%E4 %b9%88%e6%98%af%e5%8f%8d%e5%b0%84%ef%bc%9f
Understanding the Java Reflection mechanism