[Java articles] simple understanding of reflection mechanism

After learning JDBC, the reflection mechanism is used, so review the knowledge points related to the reflection mechanism;


    The Java reflection mechanism is an important feature of the Java language. Before learning the Java reflection mechanism, you should first understand two concepts, compile time and run time.

  • The compilation period refers to the process of handing over the source code to the compiler to compile it into a file that the computer can execute. In Java, it is the process of compiling Java code into class files. During the compilation period, only some translation functions are performed, and the code is not put into memory to run, but only operated as text, such as checking for errors.
  • The runtime is to hand over the compiled file to the computer for execution until the end of the program. The so-called runtime puts the code on the disk into the memory and executes it.

  The Java reflection mechanism is in the running state. For any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties; this dynamic acquisition of information and dynamic calling of object methods A feature called the reflection mechanism of the Java language. Simply put, the reflection mechanism refers to the fact that a program can obtain its own information at runtime. 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.

  The Java reflection mechanism is widely used in server programs and middleware programs. On the server side, it is often necessary to dynamically call a specific method of an object according to the client's request.

How to understand reflection?


  In general, when we use a certain class, we will know the class in advance and what to use it for. We can instantiate and create the object directly through the new object, and then use this object to operate on the class. This is orthographic

  Reflection is that you don't know what class to initialize at the beginning, and you can't use new to instantiate and create objects. It is mainly realized through the reflection API provided by JDK. You only know what class you want to operate at runtime, and you can Get the complete structure of the class and call the corresponding method, which is reflection ;


Summarize


1. What is the Java reflection mechanism?

   The reflection mechanism of Java means that in the running state of the program, objects of any class can be constructed, the class to which any object belongs, the member variables and methods of any class can be known, and the method of any object can be called. properties and methods. This function of dynamically obtaining program information and dynamically calling objects is called the reflection mechanism of the Java language. Reflection is considered the key to dynamic languages.
  To put it more simply, when a Java program is running, a reflection object of a class is created in a non-new way, and then related operations are performed on the class, such as:

  • Get the member variables of the object & assign
  • Call the object's method (including constructor, with or without parameters)
  • Determine the class the object belongs to

2. The three ways and differences of obtaining Class s in the Java reflection mechanism?


Three ways:

  • Get it through MyClass.class
  • Obtained by Class.forName("global naming of the class")
  • Obtained by new MyClass().getClass()

Method comparison:

  • Obtained through MyClass.class, the JVM will use the ClassLoader class loader to load the class into memory, but will not do any class initialization work, and return the java.lang.Class object
  • Obtained by Class.forName("global naming of the class"). Similarly, the class will be loaded into the memory by the JVM, and the static initialization of the class will be performed, and the java.lang.Class object will be returned
  • Obtained through new MyClass().getClass(), this method uses new for instantiation, so == static initialization and non-static initialization will work ==, getClass method belongs to the method in the top-level Object class, any subclass Objects can be called, which subclass calls, and returns the java.lang.Class object of that subclass

  In these three ways, the java.lang.Class objects of the corresponding class in the JVM heap area finally belong to the same one, that is, the memory address is the same, and the result of == double equal sign comparison is true;


 The content in the article is my own understanding and obtained from Baidu Encyclopedia or some articles, and it is only for my own review in the future. If there is any infringement, please contact me to delete it;

Guess you like

Origin blog.csdn.net/m0_64231944/article/details/128575574