Java Reflection Technology

Source: Internet
Author: User
1. What is Reflection
Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class;
For any object, it 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

  1. Three ways to get a class object
    Class name.
    The instance object. GetClass ()
    Class.forName ("Full path of the class");
  1. Get a constructor object from a class object
    Constructor [] cs = clazz.getconstructors (); --Get an array of constructors
    Stu Stu = (Stu) clazz.newinstance (); --Call an empty construction method to get the instance object
    Constructor C = Clazz.getconstructor (type. Class); --Gets the constructor object by specifying the parameters

  2. Get a Property object from a Class object
    Field f = clazz.getfield ("num"); --Get the Public Property object
    Field f = clazz.getdeclaredfield ("attribute"); --Get a public or private Property object

  3. Gets the object of the method through the class object
    Method m = Clazz.getmethod ("Method name", parameter type. Class); --Get a common method object
    Method m = Clazz.getdeclaredmethod ("Method name", parameter type. Class); --Get the object of a public or private method

Java Reflection Technology

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.