Java Reflection instances (a variety of ways to understand the JDK) __java

Source: Internet
Author: User

What is reflection. What is the use of reflection. 1. What is reflection.

Reflection (Reflection) enables programs that run in the JVM to detect and modify run-time behavior. "This concept is often confused with introspection (introspection), and here is the interpretation of the two terms in Wikipedia: Introspection is used to detect the type of an object and the attributes it contains at run time; reflection is used to detect and modify the structure and behavior of an object at run time.

As can be seen from their definition, introspection is a subset of reflection. Some languages support introspection, but do not support reflection, such as C + +.

Introspection Example: The instanceof operator is used to detect whether an object belongs to a particular class.

1 2 3 4 if (obj instanceof Dog) {Dog d = (Dog) obj; D.bark (); }

Reflection Example: the Class.forName () method can get the corresponding class object from the name of the class or interface (a string or fully qualified name). The Forname method triggers the initialization of the class.

1 2 3 4 5 Use reflection class<?> C = Class.forName ("Classpath.and.classname"); Object dog = C.newinstance (); Method m = C.getdeclaredmethod ("Bark"

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.