Java Reflection feature One

Source: Internet
Author: User

Tag: ADC size reflection for memory pre. Class generic Generation

A. Class understanding
        /         * * Class is the source of reflection         * Create a class, compile (Javac.exe), generate the corresponding. class file, and then use Java.exe to load (completed by the JVM's ClassLoader) this. class file.         * When this. class file is loaded into memory, it is a runtime class that is stored in the cache.         * then this runtime class is itself an instance of class.         * 1. Each runtime class is loaded only once;         * 2. Once you have an instance of class, you can do the following:         *    1> Create objects of the corresponding runtime class         *    2> Gets the complete structure of the corresponding run class (properties, methods, constructors, inner classes, parent classes, packages, exceptions, annotations 、...         *    3> calls the corresponding runtime class for the specified structure (properties, methods, constructors)         *    4> Reflection application: Dynamic Agent         */

A method of acquiring a class object

1) obtained by class's Static property class

Class clazz = Person.class;

2) obtained by the GetClass method of the runtime class

        person person = new person ();        Class clazz = Person.getclass ();    

3) obtained by the static method of class

        String className = "Hello." Person ";//The fully qualified name of the run-time class, class        Clazz = Class.forName (className);

4) obtained through the class loader (learn)

        String className = "Hello." Person ";//The fully qualified name of the class        ClassLoader ClassLoader = This.getclass (). getClassLoader ();        Class clazz = Classloader.loadclass (className);

  

Second, create an instance object of the corresponding run-time class through the class object
        Class Clazz = person.class;//does not use generics, default is the class object of object type person person        = (person) clazz.newinstance ();  A strong turn is required, and the method defaults to calling the run-time class's parameterless construction method, so the class must have no parameter construction method, the permission is large enough        class<person> clazz1 = person.class;// A generic is used that specifies the class object of the person type person        Person1 = clazz1.newinstance ();//does not require a strong turn

Java Reflection feature One

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.