Java "reflection" and java reflection
I remember a long time ago, I saw in a blog post that "reflection" is a major essence of Java. At that time, I still don't understand it. Now, more and more projects are involved, so I can feel the wonderful use of reflection.
What is reflection?
Reflection in java refers to the reflection mechanism.: JAVA has a very prominent Dynamic correlation mechanism: Reflection, used in Java, refers to the classes that we can load, explore, and use completely unknown during runtime compilation. In other words, a Java program can load a class whose name is known only at runtime, and learn its complete structure (but does not include the definition of methods ), and generate its object entity, set its fields value, or arouse its methods.
Well, I didn't know it was a human voice.
Let's take a look at our ideas: we all know that Java has to go through two phases from writing to running: Compilation and runtime,
When the above two sentences are added to the blacklist, you do not need to determine the type of Class when compiling the class file. For example: Take the BaseDaoImpl mentioned in my previous blog post <T> (for details, see blog: http://blog.csdn.net/kklt21cn/article/details/42040597)
In the class file generated after compilation, there is no reference to the object of the entity class, in other words, the object to be called is unknown when the class file is not running.
However, we need to know the name of the entity we call at runtime, because we need to know the name before we can write its hql statement. How can we achieve this? The answer is reflection!
Actually, in java, we can understand it as a class object, which is a class object ). When we can get the class object, we can run all its methods through reflection of the class object, or obtain its attributes (field ). In this way, you don't have to determine the type during the compilation period when writing code. You can wait until the runtime.
What can reflection do?
The usefulness of reflection is actually everywhere in our daily code. For example, the BaseDaoImpl I mentioned earlier can reduce a lot of redundant code, such as components in the framework, such as: ommons-lang.jar. Here is a tool class BeanUtils that I prefer to use. Here, a value assignment method copyProperties (Object source, Object target) is provided ). Reflection is used to assign the value of a javaBean to another JavaBean. Reflection can also be used to complete annotation. If you are interested in annotation, please refer to the link http://www.zhihu.com/question/24401191. In the first answer, Daniel has explained how to implement it from the bottom layer.
In fact, reflection is only one of the features of the Java language. I personally feel that I have gradually understood these advanced features, so that I have entered a language. We can also think about the value of a language from the perspective of language design. Only in this way can we be regarded as a real engineer, not just a code farmer who will copy and paste it!
If any, please correct me.