A brief analysis of Java reflection mechanism

Source: Internet
Author: User

Java reflection mechanism is in the running state, for any class , can know all the properties and methods of this class, for any one object , can call any of its methods and properties This dynamic acquisition of information and the ability to dynamically invoke the object's methods is called the reflection mechanism of the Java language.

The Java reflection mechanism mainly provides the following purposes:

    • Determine the class to which any object belongs at run time
    • To construct an object of any class at run time
    • Determine the member variables and methods that any one class has at run time
    • Methods to invoke any object at run time
    • * Using the reflection mechanism of Java, it is generally necessary to follow three steps:
      1. Get the class object you want to manipulate
      2. The class object obtained by the first step gets the method or property name of the operation
      3. The method or property obtained in the second step of the operation

      When Java runs, a class, regardless of how many objects are generated, will correspond to the same class object, which represents the classes and interfaces in the program that are running. There are three ways to get the class object for the action classes:

      1. Call the static method of class forname;
      2. Use the class's. class syntax, such as:class<?> cls = String.class;
      3. Call the GetClass method of the object, such as: String str = "ABC";class<?> CLS = str. getclass ();

A brief analysis of 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.