What is the meaning of java reflection mechanism ah

What is reflection in JAVA

JAVA reflection mechanism is in the running state, for any class, are able to know all the attributes and methods of this class; for any object, are able to call any of its methods and attributes; this dynamic access to information as well as the dynamic invocation of the object’s methods is known as the reflection mechanism of the java language.

JAVA reflection (radiation) mechanism: “program runtime, allowing changes in program structure or variable types, this language is called a dynamic language”. From this point of view, Perl, Python, Ruby are dynamic languages, C++, Java, C# are not dynamic languages. But JAVA has a very prominent dynamic-related mechanism: Reflection, which is used in Java to mean that we can load, explore, and use classes at runtime that are completely unknown at compile time. in other words, a Java program can load a class whose name is only known at runtime, learn about its full constructs (but not its methods definitions), and generate its object entities, methods, and objects. In other words, a Java program can load a class whose name is known at runtime, learn its full constructs (but not its methods definition), and generate its object entities, or set values to its fields, or evoke its methods.

Expansion information:

What is Java’s reflection mechanism?

Java’s reflection mechanism is one of the very key mechanisms that make it dynamic and is a feature that is widely used in JavaBeans.

The most common problem in utilizing JavaBean is to get an instance of the class based on the specified class name, class field name and the corresponding data, an example below demonstrates this implementation.

-|Base.java//Abstract Base Class

|Son1.java//Base Class Extension 1

|Son2.java//Base Class Extension 2




* create2005-4-149:06:56



(1) Base.java abstract base class is just a definition



(2) Son1.java/ Son2.java is the implemented JavaBean

















System.out.println (s);





















(4) Util.java demonstrates how to get a salary based on a specified class name. How to get an instance of a class based on the specified class name, class field name and the corresponding data



// The biggest advantage of this method is that there is no class name Son1,Son2 can be specified by parameters. specified, the program does not even need to appear inside


Basebase = null;;





Class[]paraTypes=newClass[]{String. Class};



method. invoke(base,paraValues);







Son1son1=(Son1)Util.convertStr2ServiceBean(“trying.reflect.Son1”, “setName”, “wangdasha”);

Java’s reflection mechanism is what, how to achieve

Java’s reflection mechanism, a common point of interpretation is to be able to run the program to dynamically obtain the information of any object in memory, this information includes the class to which the object belongs to, the methods and properties of the class, as well as their access control domains and the type of the return value, etc., but also through the reflection of the object to dynamically invoke methods, regardless of whether the method access domain is private or public, including constructor methods, but also to achieve dynamic proxies. Methods can also be called dynamically through reflection, regardless of whether the access domain of the method is private or public, including constructor methods, but also to achieve dynamic proxies and so on. In short, reflection can break the encapsulation of the JAVA class itself, and then obtain its private or public information, and also break the encapsulation and call private or public methods.

The realization of the words is through the reflection interface, JAVA to reflect the relevant class interfaces are encapsulated in the java.lang.reflect this package, you can study this package of classes, for each attribute of the class, such as variables, methods, methods, constructor methods, this package are corresponding to the class, through the class can be operated by the attributes of this class.

java reflection is very powerful, but also very dangerous, in the actual development should be used sparingly or not, in the necessary to use, often also can solve the problem you encountered.

What is Reflection? What Java reflection?

What does java reflection mean? Below takes you through it.

JAVA reflection is an ability of a program to access, detect and modify its own state or behavior. Reflection is a powerful tool to create flexible code that can enable code to be assembled at runtime without the need for source-representation links between components.

JAVA reflection mechanism is in the running state, know all the properties and methods, for any object, the ability to call its methods and properties, this dynamically acquired information and the ability to dynamically call the object’s methods of the function of the reflection mechanism.

Where is reflection appropriate

1. Creating objects based on the type new

2. Defining variables based on the type, which may be a basic type or a reference type, a class, or an interface

3. Passing an object of the corresponding type to a method

4. Accessing the attributes of an object based on the type, calling methods, and so on

These operations are based on the type of the object. p>

These operations are the process of data manipulation is the most common and the most difficult to reuse the optimization of the place, but if the operation of the use of reflection here can be achieved dynamically operate different types of instances, through the call to reflect the entry class Class, access to the corresponding attributes, constructs, methods to complete the corresponding operation.