Java reflect mechanism

Source: Internet
Author: User
Tags reflection wrapper

Java reflection mechanism (Java reflect):

In the Java Runtime environment, for any class, you can tell what properties and methods the class has. For any object, can call his method. The answer is yes, this dynamic acquisition of class information, and the ability to dynamically invoke a class's method comes from Java reflection. So that Java has the characteristics of dynamic languages.

The Java launch mechanism mainly provides the following functions:

1, at run time to determine the class to which any object belongs

2. To construct an object of any class at run time

3. Determine the member variables and methods that are available to any class at run time (you can even invoke the private method by reflection)

4. A method of calling any object at run time (note to the President: The premise is at run time, not at compile time)

Introduction to Java Reflection-related APIs:

Located in the Java.lang.reflect package

--class class: Represents a Class

--filed class: Represents a class's member variable

--method class: Ways to Represent classes

--constructor Class: Representative class construction method

--array class: Provides a static method for dynamically creating arrays and accessing elements of an array. All of the methods in this class are static methods

------Class Classes

The Java object class declares several Methods:hashcode (), Equals (), Clone (), toString (), GetClass (), and so on, in all Java classes, where the GetClass () Returns an object of type class.

Class classes are very special, and they are inherited from object like normal classes, which are used to express the class and interface of Java program runtime, and also to express Enum,array,primitive,java types and keyword void. When a class is loaded, or when the defineclass () of the Loader (class loader) is invoked by the JVM, a class object is generated, class is reflection origin, for any class (class) you want to explore, The only way to create a class object for him is to generate more than 10 reflection APIs through the latter.

Java allows us to generate corresponding class objects from a variety of ways for class class.

--Using GetClass (): Methods in the object class, each class has this method String str = "ABC"; Class cl = Str.getclass ();

--Use the Class.getsuperclass (): Method in class classes to return the class

--using Class.forName () static method:

--use. Class: Classes name. class

--Using primitive wrapper classes Type syntax: Basic type wrapper class type, such as: Integer.type (note the use of type, only native (basic) data type)

--Run-time Generation instance, want to generate objects of the entity, there are two methods in the reflection dynamic mechanism, one for the invariant construction method, a construction method with parameters, if you want to call with the parameters of the construction method, it is more troublesome, can not directly call the class Newinstance () instead of calling the Newinstance () method in the constructor class, first prepare a class[] as the constructor parameter type. The Geiconstructor () method of the class object is then invoked to obtain an exclusive constructor object, and finally a object[] as an argument to the Newinstance () method as the constructor object.

What you need to note here is that only two classes have the Newinstance () method, class class and constructor class, where the Newinstance () method of the class class is not parametric, The Newinstance () method in the Constructro class is parameterized.

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