Reflection of Java Learning Notes

Source: Internet
Author: User

Ruchunli's work notes , who says programmers can't have a literary fan?


Java programs can dynamically load, parse, and use some types that are not deterministic during the compilation phase, known as reflection (Reflection) or introspection (introspection). Java programs 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 Toexamine itself) is called introspection (introspection, vipassana, introspection). Reflection and introspection are the two terms that are often and are mentioned.


Class is the core class in the reflection API. When the class loads, the Java virtual opportunity automatically creates the corresponding class object.

The GetClass () method is defined in the Java.lang.Object class, so for any Java object, the class of the object can be obtained by this method.
Instances of class classes are used to represent runtime Java data types, including classes, interfaces, arrays, enumerations, annotations, basic data types, and even void.

Use the Reflection steps to summarize:

First, get the Java.lang.Class object of the class you want to manipulate

Get the Class object:

    • For reference data types:

Call static method: Class.forName ();

Class.forName ("P1. Person "); Class.forName ("Com.mysql.jdbc.Driver");

Call the GetClass () method defined in the object class

person person = new person (); Class cs = P.getclass (); Class strclass = "Hello World". GetClass ()

Using the. Class expression

Class strclass = String.class; Class Personclass = p1. Person.class; Class jdbcclass = Com.mysql.jdbc.Driver.class;
    • For basic data types and void

Using the. Class expression

Class intclass = Int.class; Class dobclass = Double.class; Class voidclass = Void.class;

Call the Type property of the corresponding wrapper class

Class integerclass = Integer.type; Class voicclass = Void.type;

Ii. Obtaining member information for a class

Invoke methods such as Getdeclaredmethods

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/90/wKiom1d1_3qzmK6GAAQgDJUsMjA120.jpg "title=" Qq20160701132742.jpg "alt=" Wkiom1d1_3qzmk6gaaqgdjusmja120.jpg "/>

iii. using the Reflection API to manipulate member information for a class

In the JDK, the Java reflection mechanism is implemented primarily by the following classes, which are located in the Java.lang.reflect package.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/90/wKiom1d2ABChBtthAAd1S-Qwee0215.jpg "title=" Qq20160701133030.jpg "alt=" Wkiom1d2abchbtthaad1s-qwee0215.jpg "/>


This article is from the "world of Stuffy Gourd" blog, please be sure to keep this source http://luchunli.blog.51cto.com/2368057/1794856

Reflection of Java Learning Notes

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.