Detailed examples of java reflection mechanism

Detailed examples of java reflection mechanism

preface

Today, let's introduce the reflection mechanism of Java. In the past, we used new to get an instance of a class. That's too low. Come with me today to learn a higher way to achieve it.

text

Java reflection mechanism definition

Java reflection mechanism means that all properties and methods of any class can be known in the running state; For any object, you can call any of its methods and properties; This kind of dynamically acquired information and the function of dynamically calling object methods are called the reflection mechanism of Java language. In a word, reflection can realize that you can know the properties and methods of any class at run time.

Advantages and disadvantages of reflection mechanism

Why use reflection mechanism? Don't you just create objects directly, which involves the concepts of dynamic and static

Static compilation: determine the type and bind the object at compile time, that is, through.

Dynamic compilation: determine the type and bind objects at runtime. Dynamic compilation maximizes the flexibility of Java, reflects the application of polymorphism, and reduces the coupling between classes.

advantage

It can dynamically create objects and compile, which reflects great flexibility, especially in the development of J2EE. For example, a large-scale software cannot be designed perfectly at one time. When the program is compiled and released, when it is found that some functions need to be updated, we can't ask users to uninstall the previous software and reinstall the new version. If so, the software must not be used by many people. If it is static, the whole program needs to be recompiled once to realize the function update. If it adopts the reflection mechanism, it can realize the function without unloading. It only needs to be dynamically created and compiled at runtime.

shortcoming

It has an impact on performance. Using reflection is basically an interpretation operation. We can tell the JVM what we want to do and it meets our requirements. Such operations are always slower than just performing the same operation directly.

Understand class and class types

To understand reflection, first understand the class class, which is the basis of reflection implementation.

Class is Java Lang. class is the instance object of class, and class is the class of all classes (there is a class named class). For ordinary objects, we generally create and represent them as follows:

As mentioned above, all classes are class objects. How to represent them? Can you do it in the following ways:

But when we look at the source code of class, it is written as follows:

It can be seen that the constructor is private and only the JVM can create class objects. Therefore, we can't create a class object like an ordinary class. Although we can't create a new class object, we can get a class object from an existing class in three ways, as follows:

This shows that any class has an implicit static member variable class, which is obtained by obtaining the static member variable class of the class

Code1 is an object of code. This method is obtained through the getClass () method of a class object

This method is obtained by calling the forname method by a class. Here, C1, C2 and C3 are class objects. They are exactly the same and have a scientific name, Class type called code (class type). It's strange here. I said that code is the object of class, and C1, C2 and C3 are also the objects of class. Isn't code the same as C1, C2 and C3? Why is it called code? What kind of class type? Don't worry about whether they are the same. Just understand what class types do. As the name suggests, class types are class types, that is It describes what a class is and what it has, so we can know the properties and methods of a class through the class type, and call the properties and methods of a class, which is the basis of reflection.

A simple example code:

Execution results:

Java reflection related operations

We know how to get a class, so what can we do with this class?

The summary is as follows:

Get member method get member variable field get constructor

Let's introduce it in detail

Get member method information

Obtaining a method separately is obtained through the following methods of class:

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>