Initial knowledge of Java reflection mechanism

Source: Internet
Author: User

In Java, everything is an object


The Java reflection mechanism is to get the properties and methods of any class in the program running. To class.forname ("Com.mysql.jdbc.Driver.class"), I believe that everyone will not be unfamiliar, until the study of the Java reflection mechanism, it dawned, the original database connection used in this sentence is the Java reflection mechanism.


There are three ways to get a class object:

Class c=class.forname (""); This is also the first time you have touched the Java reflection mechanism while learning Factory mode, and then viewed the Java reflection mechanism from the Internet.

Second, Class c=new String (""). GetClass ();

Third, Class C=shape.class


Classes required for class reflection in Java field, Constructor, Method, class, Object

Field class: Provides information about the properties of a class or interface, and the dynamic access rights to it. The reflected field may be a class (static) attribute or an instance property, and a simple understanding can be seen as a class that encapsulates the properties of a reflective class.
Constructor Class: Provides information about a single construction method of a class and access to it. Unlike field classes, the field class encapsulates the properties of the reflection class, while the constructor class encapsulates the method of constructing the reflection class.
Method Class: Provides information about a single method on a class or interface. The method that is reflected may be a class method or an instance method, including an abstract method. This class is not difficult to understand, it is a class used to encapsulate the reflection class method.
Class : An instance of a class represents classes and interfaces in a running Java application. An enumeration is a class, and a comment is an interface. Each array belongs to a class that is mapped to a class object, and all arrays that have the same element type and number of dimensions share the class object.
Object class: Each class uses object as a superclass. All objects, including arrays, implement methods of this class.

use of the Java reflection mechanism

    1. Determine the class to which any object belongs at run time
    2. To construct an object of any class at run time
    3. Determine the member variables and methods that any one class has at run time
    4. Methods to invoke any object at run time

Initial knowledge of Java reflection mechanism

Related Article

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.