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