Java Reflection mechanism

Source: Internet
Author: User

Java Reflection Mechanism

First,

1, understand the role of reflection

2. Master the operation and use of class

3. You can obtain an instantiated object of a class by reflection

4. The structure of the class can be obtained by reflection, and the invocation of the property method can be made by reflection

ii. recognition of reflection

Java The reflection mechanism is like a mirror, looking in the mirror to discover all of its characteristics, and a class can find all its own methods by reflecting (looking in the mirror).

The reflection mechanism is used especially in the frame above! (How is it applied?) )


For example, JavaScript is a dynamic language with the following code:

Var a=1;

A= "Weiwei";

A=false;

This three lines of code shows that a can be dynamically changed at run time, changed from int type to string type, and string type to Boolean type, that is, when the program is run, it is allowed to change the program structure or variable type, which is dynamic language. And the Java, C + + equation does not allow this if a is an int type, it cannot be changed arbitrarily. So, Java is a static language.

Explanation: Class.forName ("Org.lxh.demo.classdemo02.Person"). The program at compile time does not know that Org.lxh.demo.classdemo02.Person is a person class, only know that it is a character channeling, only when the program is running to know it is a man class, that we can and runtime loading, detection, Use classes that are completely unknown during compilation.


As follows: Using the Getdeclaredmethods () method, you can know what methods are declared in a class with a known name, including private methods.


Normally, if a class is to be used, you must find this class, and then instantiate an object through this class, you must first have the class and then the object, so now if you want to find the class by an object?

Iii. Understanding Class class

in the reflection mechanism, the class class is the source of an operation, and all the reflection operations are expanded from this class, but there are three ways to instantiate objects of this class;

1. The GetClass () method of the object class

2, through the form of class, such as: Person.class (second most commonly used)

3, Class.forName () (most commonly used)

Iv. Adoption of Class Class Instantiation Object

in the Class class, instantiating an object is one of the most commonly used operations, and the implementation of all future framework programs depends on Class class to complete.

1. instantiate a class without a parameter construct

If there is an argument-free constructor in a class, you can instantiate it directly from the class.

The following code is available:

Step: 1, get the class object

2. Examples of production classes

Package Org.lxh.demo.classdemo02;public class Person {public String toString () {return ' Hello World!!! ';}} public class InstanceDemo01 {public static void main (string[] args) {class<?> C = null;try {c = class.forname ("Org.l Xh.demo.classdemo02.Person ");//First Instantiate object} catch (ClassNotFoundException e) {e.printstacktrace ();} try {person P = (person) c.newinstance ();//Create Object System.out.println (p);} catch (Instantiationexception e) { E.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();}}}





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.