Reflection of the Java Foundation (i)

Source: Internet
Author: User
Tags wrapper

in the Java Runtime environment, for any class, you can know the properties and methods of the class, instantiate the object, and instantiate the objectThe ability to get information about a class and to dynamically invoke the object's methods comes from the reflection (Reflection) mechanism of the Java language. So what does reflection do for you? 1. At run time, judge the class to which any object belongs. 2. Constructs an object of any class at run time. 3, at run time to judge any one class has the member variables and methods. 4. A method that invokes any object at run time. in general, the dynamic language spoken by the developer community is broadly agreed to as a definition: "When the program is running, it allows you to change the program structure or variable type, which is called a dynamic language."    "From this point of view, Perl,python,ruby is a dynamic language, and c++,java,c# is not a dynamic language. A Java program can load a runtime to know the name of a class, learn its full construct, 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). Reflection is a key property of Java as a dynamic (or quasi-dynamic) language. 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, which is the core class in the Reflection API. Field class: Represents the properties of a class. method Class: Methods that represent classes. Constructor class: Represents the construction method of a class. Array class: Provides a static method for dynamically creating arrays and accessing array elements. the GetClass () method is defined in the Java.lang.Object class, so the type of the object can be obtained by this method for any Java object.    Class method:getName (): Gets the full name of the class.    getfields (): Gets the properties of the public type in the class. getdeclaredfields (): Gets all the properties of the class. getmethods (): Gets the method of all public types in the class. getdeclaremethods (): Gets all the methods of the class. getconstructors (): Gets the constructor method for the class public type. GetMethod (String name,class[] parametertypes): Gets the class's specific method, the name parameter specifies the method's name, and the Parametertypes parameter specifies the method's parameter type. GetConstructor (class[] parametertypes): Gets the specific constructor method for the class, and the Parametertypes parameter specifies the type of the parameter for the constructed method. newinstance (): Creates an object of this class from the constructor of the class without parameters. Field class method:get (Object obj): Gets the value of the current property in obj.    Set (Object obj,object value): Sets the value of the current property in obj to values. In addition, the field class provides the GetXXX () method family and the Setxxx () method family for manipulating basic data types. Last Example:   method class is used to manipulate classes. Where the method class's invoke (Object Obj,object args[]) methods receive a parameter that must be an object, if the parameter is of the base type data, the object must be converted to the corresponding wrapper type. The return value of the Invoke () method is always the object, and if the return type of the actual called method is the base type data, then the Invoke () method converts it to an object of the appropriate wrapper type and returns it.    Example:   

Reflection of the Java Foundation (i)

Related Article

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: 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.