Advantages:
A, the reflection mechanism of Java is to increase the flexibility of the program, to avoid writing the program to die in the code.
Example: instantiating a person () object, not using reflection, requires new person (); If you want to instantiate another class, you must modify the source code and recompile.
Use reflection: Class.forName ("person"). newinstance (); And this class description can be written to the configuration file, such as **.xml, so if you want to instantiate another class, just modify the configuration file "class description", There is no need to re-modify the code and compile. used when loading database drivers in Javaweb.
b, increase the flexibility of the program.
For example: Struts. The dispatch control of the request.
When the request comes. Struts by querying the configuration file. Locate the action method that corresponds to the request. Then instantiate the action by reflection. and call the response method. If reflection is not applicable, then you will only be able to write dead in the code.
Reflections are generally used more in frames. Because the framework applies more cases. Requires a higher degree of flexibility.
Disadvantages:
The use of reflection will reduce the performance of our software and increase the complexity, so we also need to use it carefully.
The role of the Java reflection mechanism