"Dynamic page" (ii) Java reflection

Source: Internet
Author: User

Java's reflection mechanism is a very important feature of the Java language, first look at the Sun official web site is how to define the reflection mechanism.


In general, translation may not be very accurate. Reflection (Reflection) is a feature of the Java programming language that requires Java programs to be checked or self-checked when running, and to manipulate internal properties. For example, it allows a Java class to obtain all of its own member names and display them. This ability to check and manipulate from within a Java class may not sound like a favorite, but in other programming languages this feature (function) does not exist at all. For example, there is no way for PASCLA, C, or C # programs to get the functions defined inside them. Another example of a real reflection is that in JavaBeans, software components can be manipulated by constructors. This constructor uses reflection to get Java properties (classes) because they are loaded dynamically .

In other words, the popular point to understand the Java reflection mechanism, is in the running state, for any class can be dynamically obtained its class properties and methods, for any one object can call it any method. This dynamic acquisition of information and the ability to dynamically invoke the object is called the Java Reflection mechanism, and this mechanism is unique to the Java language, which is not available in other programming languages.

Many open-source frameworks now use reflection mechanisms, such as Hibernate and struts, which are implemented using reflection mechanisms. But why should we use a reflection mechanism? Think about how we used to do this, when compiling a type, such as a class or an object, is written dead, fixed write, and then instantiate the object or to call a method, this is called static compilation. Then the corresponding is the dynamic compilation, which is the Java reflection of our blog, its implementation principle is to run the time to determine the type, and then bind the object. This dynamic compilation feature greatly limits the power of the Java language, enhances the flexibility of Java, and embodies the application of polymorphism.

    if that's what you don't understand, change the way it is. The reflection of Java refers to the information that can be obtained when the program is run, and in Java as long as the name of the class is given, all the information of the class can be obtained through the reflection mechanism. Think of the reflection mechanism we use in OA because we are not sure which entity to pass in, whether it is a role or apartment or user, we use the reflection mechanism of Java to Class<t> remember the scene. Is that we have three tables, Role, apartment and user, each table has an increase and deletion of four operations, we do not understand the Java reflection mechanism, we may have to create a different DAO class according to different tables, like us above, we have to create three DAO class, inside a lot of repeating the same method, Just the class name is not the same.

Now with the reflection mechanism of Java, we have to jump out of the old circle. Think of human is really smart, develop tools also forget, also want to constantly improve how can reuse high, high efficiency. So we now use the Java reflection mechanism, only to create a DAO class, and then the method of adding and removing changes to write in, but the type is empty, then as long as the transfer of different types of objects can be manipulated, without the number of tables to create how many DAO class, the reflection mechanism will automatically help us do this thing. Like a wine cellar with three vats, each with blue wine, green wine and yellow rice wine, the cellar owner puts a vats at the door every day to sell liquor. The former practice is: for example, selling blue wine today, the vats to move the blue wine in the doorway, buy green wine to carry green wine vats to the door. Later a man thought of a way, is to put an empty vats in the doorway, Ann a faucet in the vats, today want to sell what wine directly to the door of the vats inside note on the line, so do not have to move back and forth vats. Oh, how stupid the boss, of course, this story is my fiction, the purpose is just to understand the Java reflection mechanism.


So back to the point, Java's reflection mechanism can be used to dynamically create objects and compile, in order to better understand, we will make an example. Now I am a class teacher, I used to be so assigned to work, small red is responsible for cleaning the glass; small black is responsible for cleaning the blackboard; Xiao Huang is responsible for the garbage, and everyone is responsible for their own work, I see no sweep, you have to remember to arrange the small red to sweep the floor ...


But this allocation is very inefficient, because I often want to arrange a job, I have to think about arranging a fixed person to do this thing. So with the Java reflection mechanism in place, I changed the way I used to schedule my work.



In this way, using the reflection mechanism of Java, you do not know what you want to new object to do what work, originally directly created objects to perform the corresponding work, now using the Java reflection mechanism, in the process of running the program, Dynamically create an object and then go to invoke any of its methods and properties to perform its functions. I wonder if you understand the reflection mechanism of Java? In the next article, I'll show you how to read the class name and attributes of a jar package through the reflection mechanism of java.


"Dynamic page" (ii) Java reflection

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.