What reflection is java?
#What reflection is java?
Reflection is one of the features of Java and is a mechanism for indirectly operating target objects.
The JAVA reflection mechanism is that in the running state, for any entity class, all properties and methods of this class can be known; for any object, any method and property of it can be called; this kind of dynamic The function of obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.
Why is reflection needed in Java? What problem does reflection solve?
In one sentence, reflection can give jvm the ability to compile dynamically. Otherwise, the metadata information of the class can only be achieved by static compilation, such as hot loading, Tomcat's Classloader, etc. cannot support
There are two types of compilation in Java:
● Static compilation: The type is determined at compile time, and the bound object is passed.
● Dynamic compilation: determine the type and bind the object at runtime. Dynamic compilation maximizes the flexibility of Java, embodies polymorphic applications, and can reduce coupling between classes.
Java Reflection is a key property of Java being considered a dynamic (or quasi-dynamic) language. This mechanism allows the program to obtain the internal information of any class with a known name through the Reflection APIs at runtime, including its modifiers (such as public, static, etc.), superclass (such as Object), implemented interfaces (such as Cloneable), and All information about fields and methods, and can change the contents of fields or invoke methods at runtime.
Reflection can load, detect, and use classes that are completely unknown during compilation at runtime. That is, a Java program can load a class whose name is known only at runtime, obtain its complete structure, and generate its object entity, or set values for its fields, or invoke its methods.
Reflection allows static languages to inspect and modify the structure and behavior of the program at runtime.
In static languages, when using a variable, you must know its type. In Java, the type information of variables is saved in the class file during compilation, so that it can be accurate at runtime; in other words, the behavior of the program at runtime is fixed. If you want to change it at runtime, you need reflection.
The classes that implement the Java reflection mechanism are all located in the java.lang.reflect package:
1. Class class: represents a class
2. Field class: represents a class Member variables (properties of the class)
3. Method class: represents the method of the class
4. Constructor class: represents the construction method of the class
5. Array class: provides To dynamically create arrays and access static methods of array elements
Use
1. Three ways to obtain Class objects
1.1 Object ——> getClass();
1.2 Any data type (including basic data types) There is a "static" class attribute
1.3 Through the static method of the Class class: forName (String className) (commonly used)
/** * 获取Class对象的三种方式 * 1 Object ——> getClass(); * 2 任何数据类型(包括基本数据类型)都有一个“静态”的class属性 * 3 通过Class类的静态方法:forName(String className)(常用) * */ public class Fanshe { public static void main(String[] args) { //第一种方式获取Class对象 Student stu1 = new Student();//这一new 产生一个Student对象,一个Class对象。 Class stuClass = stu1.getClass();//获取Class对象 System.out.println(stuClass.getName()); //第二种方式获取Class对象 Class stuClass2 = Student.class; System.out.println(stuClass == stuClass2); //判断第一种方式获取的Class对象和第二种方式获取的是否是同一个 //第三种方式获取Class对象 try { Class stuClass3 = Class.forName("fanshe.Student"); //注意此字符串必须是真实路径,就是带包名的类路径,包名.类名 System.out.println(stuClass3 == stuClass2); //判断三种方式是否获取的是同一个Class对象 } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("*****************获取公有、无参的构造方法*************"); Constructor con = clazz.getConstructor(null); //1>、因为是无参的构造方法所以类型是一个null,不写也可以:这里需要的是一个参数的类型,切记是类型 //2>、返回的是描述这个无参构造函数的类对象。 System.out.println("*************获取公有字段**并调用*****************"); Field f = stuClass.getField("name"); System.out.println(f); System.out.println("***************获取私有的show4()方法******************"); m = stuClass.getDeclaredMethod("show4", int.class); System.out.println(m); m.setAccessible(true);//解除私有限定 Object result = m.invoke(obj, 20);//需要两个参数,一个是要调用的对象(获取有反射),一个是实参 System.out.println("返回值:" + result); } }
The above is the detailed content of What reflection is java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
