Java Reflection mechanism

Source: Internet
Author: User

Introduction: The reflection mechanism of Java is one of the Java features, and the reflection mechanism is the foundation of the framework technology. A flexible grasp of the Java reflection mechanism, for everyone to learn the framework technology has a great help.

1. What is the Java reflection mechanism

Simply put, the Java reflection mechanism, in the running state, is able to access all the properties and methods of the class for any class, and any object can also invoke any of its methods and properties, which is called the Java language's reflection mechanism. In a word, the class has what information, it can get what information, but the premise is to know the name of the class, or there will be no later text.

2. Where to use the reflection mechanism

(1) A line of code was used to learn JDBC, class.forname ("Com.mysql.jdbc.Driver.class"). newinstance (); But then only knew that the line of code is to generate the driver object instance, do not know its specific meaning. This is actually reflection.

(2) Everyone has used JCreator and eclipse. When we build an object, we call the method and properties of the object. At the click of a point, the compilation tool automatically lists all the methods and properties that the object can use for the user to select. This is the use of the principle of Java reflection, is to create the object of our discovery, self-examination.

Now many open frames are used in the reflection mechanism, hibernate and struts are implemented by reflection mechanism.

3. What can I do with the reflection mechanism?

At first, when using JDBC, when writing access to the database to write to want to vomit, there are eight tables, each table has an add-and-remove operation, at that time do not know the concept of reflection mechanism, so on the different tables to create a different DAO class, so not only the development rate, but also the code redundancy is very bad, The most deadly is to look at the same, and then directly copy the changes, because it is easy to make a variety of low-level errors (case, one or fewer letters AH ...). ), an error can make you look for half a day.

With the Java reflection mechanism, what is good to do, just to write a DAO class, four methods, adding and removing changes, passing in different objects, OK, no need to create a DAO class for each table, the reflection mechanism will automatically help us complete the rest of the matter, which is its advantage. Frankly speaking, the reflection mechanism is specifically to help us do those repetitive and regular things, so now a lot of automatic code generation software is the use of reflection mechanism to complete.

How the 4.JAVA bottom layer implements the reflection mechanism

The implementation of the reflection mechanism of Java is based on 4 classes: Class,constructor,field,method (the keyword class is actually a class, initialized by the JVM)

  

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.