Java Reflection mechanism

Source: Internet
Author: User

1, what is the reflection mechanism?

Simply put, the reflection mechanism is worth the information that the program can acquire itself at run time. In Java, all information about a class can be obtained by the reflection mechanism as long as the name of the class is given.


2. What functions does the Java reflection mechanism provide?

Ability to determine the class to which any object belongs at run time

To construct an object of any class at run time

Determine the member variables and methods that any one class has at run time

Methods that invoke either object at run time

To create a new class object at run time


3. Where is the reflection mechanism used?

There is one line of code in JDBC: Class.forName (' Com.mysql.jdbc.Driver.class '). newinstance (); At that time only knew the generation Drive object instance, later knew, this is the reflection, Many frameworks now use reflection mechanisms, and hibernate,struts are implemented using reflection mechanisms.


4, the advantages and disadvantages of reflection mechanism?

Advantages:

can achieve dynamic creation of objects and compilation, showing great flexibility, especially in the development of the Java EE translated and released, when it was discovered that some features needed to be updated, We could not have the user put the previous uninstall, and then reinstall the new version, if        

< Span style= "FONT-FAMILY:HELVETICA,TAHOMA,ARIAL,SANS-SERIF; line-height:25.1875px; " > Cons:

have an impact on performance. Using reflection is basically an interpretation operation, and we can tell the JVM what we want to do and it satisfies our requirements. This type of operation is always slower than just performing the same operation directly.

Static compilation: Determines the type at compile time, binds the object, that is, by

Dynamic compilation: The runtime determines the type, binding object. Dynamic compilation maximizes the flexibility of Java and embodies the application of polymorphism, which helps to reduce the coupling between classes.



Quoted: http://blog.csdn.net/snn1410/article/details/44978457


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.