The reflection mechanism in Java

Source: Internet
Author: User

First, Reflection

a) refers to a class that can be used for runtime loading, discovery, and use of completely unknown during compilation

Second, Characteristics of Reflection

a)          program running state, you can dynamically load a class with only one name loaded class All methods and properties of this class for any class object,

Third, Reflective notation ( Gets an object of that class through reflection )

a) person person= (person) class.forname ("Com.example.Person");

Four, for any class that you want to dynamically load, run the class, you must first obtain the corresponding Class

Five, Get Class the method

a) getclass ();

b) class.forname ();( most commonly used )

c) . Class

Six, the effect of the reflection mechanism:

a) Dynamically loading classes, dynamically fetching the information of the class (properties, methods, constructors);

b) dynamically constructing objects

c) Any method, constructor, that dynamically invokes classes and objects

d) dynamic invocation and Processing properties

e) get generic information

f) handling annotations

Seven, how the reflection mechanism introduces generics

a)          java Span style= "font-family: Song body; Font-size:18pt "", java javac

i. Ensure that data security is exempt from the hassle of forcing class conversions.

b) Once the compilation is complete, all types related to generics are wiped out

Eight, performance problems of reflection mechanism

a) setaccessible switch for security check

                          i.               Java language access check

                        ii.                Java language access check. Not for true

You can access the false will not be accessible.

b) Improved reflection efficiency if security checks are disabled

Nine, in order to cater for actual development through reflection, Java has added

a) parameterizedtype: represents a parameterized type (for example, collecion<string>)

b) genericarraytype: an array type that represents an element type that is a parameterized type or a type variable

c) typevariable: Common Parent interface for various types of variables

d) wildcardtype: represents I a wildcard type expression

The reflection mechanism in Java

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.