Java reflection mechanism--what, why, how to use __java

Source: Internet
Author: User
1. What is Reflection

First reading to understand reflection, it is difficult to fully understand, this is related to Java language features, JVM memory details, I looked at the reflection, as the high vote answer, according to write again, the result is that you do not understand, you are just suit, to really understand, I suggest the main topic, and constantly in-depth inquiry, When you step deep enough back, you will be good.

Here I'm not going to be like the other answer up on the code, to tell you how to use, what is what is, to be honest, it is more like training Java programmers feel, a little knowledge of the curiosity is not, if you want to reflect the crash, we see other answers.

Write the answer, there may be a default knowledge phenomenon, is some knowledge point, I know, but you do not know, you can comment. Here I try to explain a wave simply and rudely.

First we understand the JVM, what is the Jvm,java virtual machine, Java is able to cross the platform because of this thing, you can understand as a process, the program, but his role is to run your code. The image above is the Java memory model, we focus on the point, a method area, a stack, a heap, beginners when the teacher is not in-depth words only tell you Java memory is divided into heaps and stacks, understandable point bar.

If you write a piece of code: Object O=new object ();

Run up.

First, the JVM will start, Your code will be compiled into a. class file, the class loader is then loaded into the JVM's memory, and your class object is loaded into the method area, creating class objects for object classes into the heap, noting that this is not a new object, but a class object, each class has only one class object, as the method area class The data structure of the interface. Before the JVM creates the object, it checks to see if the class is loaded, looks for the class object, and, if loaded, allocates memory for your object, initializing the Code: new Object ().

The above process is your own written code thrown to the JVM to run, run over, the JVM shut down, your program also stopped.

Why are you talking about this? Because to understand reflection you must know what scenario it is used in.

The main thought of the above program object is his new, the program is equivalent to write dead to the JVM to run. If a server suddenly encountered a request oh to use a class, alas but not loaded into the JVM, is not to stop to write their own code, new, OH Start the server, (brain residue).

What is reflection? When our program is running, we need to dynamically load some classes that might not have been used before, so we don't have to load them into the JVM, but we load them as needed at runtime, which is self-evident for the server, for example, the bottom line of our project is sometimes MySQL, sometimes Oracle, It is necessary to dynamically load the driver class according to the actual situation, and this time the reflection is useful, assuming Com.java.dbtest.myqlconnection,com.java.dbtest.oracleconnection These two classes we want to use, this time our program is written more dynamic, through class TC = Class.forName ("com.java.dbtest.TestConnection"); let the JVM find and load the class on the server with its full class name, and if it is Oracle, the incoming argument becomes another. At this point you can see the benefits of reflection, this dynamic is reflected in the characteristics of Java. For example, if you're in touch with spring, you'll find that when you configure a variety of beans, you configure it as a configuration file, you need to use which beans to match, and the spring container is dynamically loaded according to your needs, and your program can run robust. 2. Use A simple example of a scene.

No reflection:

Suppose there is a production environment in which the database connection is MySQL, so the code should be this way, written in pseudocode, ignoring details
Conn =new mysqlconnection ()
However, a day suddenly changes to Oracle. So, what you have to do is change to
Conn =new OracleConnection ()
or you be smart, create a new profile, fill it with MySQL or Oracle, and then get a string of configuration files in your code, if it's MySQL conn = The new Mysqlconnection () is Oracle conn =new OracleConnection,
then. The most important thing is. You want to stop the production environment, Javac the Java code with the compiler again, and then the compiled class file to the production environment. Let's take a look at the reflection,
at the very beginning, if we think about it. Sometimes we switch the database, we write the configuration file, and then we use Class.forName (str) for the new database driver, and when we change the database, we just need to change the configuration file simply. This does not require recompiling the code, but it seems that the production environment needs to be restarted. Oh, it's OK to switch the database without restarting the production environment (regardless of efficiency) and build the wheels yourself.
The difference between reflection and no reflection here is whether you want to recompile it again. These are my thoughts and opinions, because I have recently learned what reflection is. have different views and opinions, welcome to "war"  

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.