The reflection mechanism of Java record -83-java language

Source: Internet
Author: User

The reflection mechanism of the Java language

In the Java Runtime environment, for any class, can you know what the properties and methods of this class are? Can any one of its methods be called for any of the objects? The answer is yes. This dynamic acquisition of information for classes and the ability to dynamically invoke objects is derived from the Java language's reflection (Reflection) mechanism.

The Java reflection mechanism mainly provides the following features:

1. Determine the class to which any object belongs at run time;

2. Constructs an object of any class at run time;

3. Determine the member variables and methods of any class at run time;

4. Method of invoking any object at run time;

Reflection is a key property of Java as a dynamic (or quasi-dynamic) language. This mechanism allows the program to obtain the internal information of any known name class, including its modifiers (such as public,static, etc.), superclass (for example, object), through the reflection APIs at runtime. The implementation of interfaces (for example, serializable) also includes all the information of fields and methods, and can change the fields content or Invoke methods at run time.

In general, the developer community speaks of dynamic language, which is generally accepted as a definition: "When a program runs, it changes the program structure or variable type, which is called Dynamic language." From this point of view, Perl,python,ruby is a dynamic language, and c++,java,c# is not a dynamic language.

From the above definition, Java is not a dynamic language, but it has a dynamic correlation mechanism. Java programs can be added to a runtime to know the class of the name, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods. This ability to "see through class" is called introspection (introspection, vipassana, introspection).

Introduction to the Java Reflection API

In the JDK, the Java reflection mechanism is implemented primarily by the following classes, which are located in the Java.lang.reflect package:

Class: Represents a Class;

Field class: A member variable that represents a class (a member variable is also called a property of a class);

Method Class: Methods for representing classes;

Constructor class: The construction method of the Representative class;

Array class: Provides a static method for dynamically creating an array, and for accessing the elements of an array.

In Java, regardless of how many objects of a class are generated, these objects correspond to the same class object.


The reflection mechanism of Java record -83-java language

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.