The role of the Java reflection mechanism

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.