Java Reflection mechanism

Source: Internet
Author: User

The Java Reflection mechanism provides the following functions: To determine the class to which any object belongs at run time, to construct an object of any class at run time, to determine the member variables and methods that any class has at run time, to invoke a method of any object at run time, and to generate a dynamic proxy.

Sometimes we say that a language is highly dynamic, and sometimes we distinguish between dynamic and static techniques and practices. We have catchy dynamic bindings, dynamic linking, dynamic loading, and so on. However, the term "dynamic" is not strictly and universally applicable strict definition, and sometimes even like object-oriented in the beginning of the field of programming, one person a number, each blowing the tune.

In general, the developer community speaks of dynamic language, which is broadly agreed to as a definition: "When a program is run, it is allowed to change 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.

Although Java is not a dynamic language under such definitions and classifications, it has a very prominent dynamic correlation mechanism: Reflection. This word means "reflection, image, reflection," which is used in Java to refer to the classes that we can load, detect, and use completely unknown during compilation. In other words, a Java program can load a runtime to know the name of a class, 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" (the ability of the program to examine itself) is called introspection (introspection, vipassana, introspection). Reflection and introspection are the two terms that are often and are mentioned.

How can Java make the dynamic features described above? This is a far-reaching topic, and this article simply introduces some concepts. The whole space is mostly about reflection APIs, which is to let the reader know how to explore the structure of class, how to create a copy of an entity for a "class that knows the name at runtime", set a value for its fields, and call its methods. This article will talk about Java.lang.Class, as well as Java.lang.reflect in the method, Field, constructor and so on classes.


Java Reflection mechanism

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.