JAVA reflection mechanism

Source: Internet
Author: User

First, what is the reflection mechanism

The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods; This dynamically acquired information and the ability to dynamically invoke the method of the object is called the reflection mechanism of the Java language.

Second, how to realize the reflection mechanism

The first thing to do is to java.lang.Class this class.

Java programs run at runtime and the Java Runtime system always identifies all objects as run-time types. This information records the classes each object belongs to. A virtual machine typically uses run-time type information to perform the correct method, and the class that holds the type information is the classes class.

Three, there are three ways to get class objects:

1. Pass the GetClass () method of the object class. For example:

Class C1 = new String (""). GetClass ();

2. Using the class class static method--forname () to achieve:

Class C2 = class.forname ("MyObject");

3. If T is a defined type, in Java, its. class file name: T.class represents the class object that matches it, for example:

Class C3 = Manager.class;

There are several important ways to do this in class:

1.getName ()

A class object describes a particular property of a particular class, and this method returns a brief description of the class in string form. For historical reasons, the class object of the array

2.newInstance ()

The method can produce an instance of its corresponding class based on a class object. It is to be emphasized that it calls the default constructor method for this class. For example:

MyObject x = new MyObject ();

MyObject y = X.getclass (). newinstance ();

3.getClassLoader ()

Returns the class loader for the class corresponding to this class object.

4.getComponentType ()

This method, for the class object of the array object, can get the class object of the corresponding object of the array's constituent elements. For example:

int[] ints = new int[]{1,2,3};

Class Class1 = Ints.getclass ();

Class Class2 = Class1.getcomponenttype ();

The Class2 object you get here should be the class object of the primitive type int.

5.getSuperClass ()

Returns the class object corresponding to the immediate parent class for a subclass.

6.isArray ()

Determines whether this class object corresponds to an array object.

Class C4 = int.class;

Class c5 = double[].class;

the advantages and disadvantages of reflection mechanism

The advantage of the reflection mechanism is that it can achieve dynamic creation of objects and compile, which shows great flexibility.
Disadvantages have an impact on performance.

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.