The benefits of the reflection mechanism of Java

Source: Internet
Author: User

In Java, reflection is loaded into the runtime dynamically. One great advantage is that you can save a lot of resources. Why, first of all, let's say that JVM,JVM is a Java virtual machine, and Java supports cross-platform because of the existence of a Java virtual machine. Program you have the following statement object O=new object (); When running, Java's virtual opportunity starts first. Compile your Java file into a. class file. Load into the memory of your JVM. Your class object loads into the method area, which generates classes of type objects (that is, objects of the class class) to be loaded into the heap as interfaces to the data structures of the method area classes. When the JVM creates the object, it will see if your class is loaded, and the object that generated your class is loaded. However, in large projects, many of them may not be available for the time being, so it is not necessary to generate objects for each class. And the reflection mechanism is the solution to this problem.

For example, the bottom of our project is sometimes MySQL, sometimes with Oracle, we need to dynamically load the driver class according to the actual situation, 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 the class TC = Class.forName ("Com.java.dbtest.TestConnection"), which allows the JVM to find and load this class in the server through the class's full class name, and if it is Oracle, the passed-in parameter becomes another. At this point you can see the benefits of reflection, the dynamics of the characteristics of the Java is reflected! For example, if you touch spring, you will find that when you configure a variety of beans, it is configured as a configuration file, which beans you need to match, and the spring container will dynamically load according to your needs, and your program will be able to run robustly.


Reflection gets to the class through the object, Employee Employee=new employee (); Employee.getclass (); GetClass is a method in the object class.

Note that the class file is the source of the reflection mechanism. Just find the class type object for the classes you are using. You can find the way to your class. You can call the method of this class.

There are three ways to get objects of class classes.


Such as


Learn from the following big-guy articles.
Old Urchin Botong Tong
Links: https://www.zhihu.com/question/24304289/answer/147529485
Source: Know


The benefits of the reflection mechanism of Java

Related Article

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.