A brief analysis of reflection mechanism in Java

Source: Internet
Author: User
Tags object object reflection

1. Understanding of the reflection mechanism
    • In the running state, all the properties and methods of the class are known to any one class
    • Any one of its methods and properties can be called for any object
    • What information is available in the class, and what information can be obtained using the reflection mechanism, but only if the name of the class is known
2. Effect of reflection mechanism
    • Determine the class to which any object belongs at run time;
    • Gets the instance object of the class at run time;
    • Access to object properties, methods, construction methods, etc. at run time;
    • The IOC in spring is implemented in conjunction with the factory model.
3. Application background of reflection mechanism

The reflection mechanism realizes the dynamic creation of objects and compiles, which shows great flexibility. For a large software, it is impossible to design the perfect one at a time, when the program compiled, released, when the discovery needs to update some features, we can not want users to uninstall the previous, and then reinstall the new version, if so, this software is certainly not many people use. Using static words, the entire program needs to be recompiled once to achieve the function of the update, and the use of reflection mechanism, it can not uninstall, only need to be dynamically created and compiled at run time, you can implement this function.

4. Example of reflection mechanism

First create a person class, including member properties, member methods.

1  Public class Person {23      Public String name; 4     5      Public void run () {6         System.out.println ("This is running!" ); 7     }8     9 }

Gets information about the person class through the reflection mechanism, and accesses and invokes member properties and member methods.

1 ImportJava.lang.reflect.Field;2 Importjava.lang.reflect.InvocationTargetException;3 ImportJava.lang.reflect.Method;4 5 /*6 * Reflection mechanism parsing in Java:7 * 1. Through the reflection mechanism, you can get the information of the class while the program is running.8 * Method: Obtained by class name. class9 * obtained by the GetClass () method of object, which is suitable for situations where the type is unknownTen * obtained by the full class name, with more One * 2. Create an instance object by reflection mechanism, get member methods, call member methods, get member properties, set properties A * Note: Reflection mechanism + Factory mode = IOC in spring -  */ -  Public classReflecttest { the  -      Public Static voidMain (string[] args) -             throwsclassnotfoundexception, Nosuchmethodexception, SecurityException, IllegalArgumentException, - invocationtargetexception, Nosuchfieldexception, Instantiationexception, illegalaccessexception { +Class Clazz =NULL; -  +         //obtained by class name. class AClazz = person.class; atSystem.out.println ("obtained by the class name:" +clazz); -  -         //obtained by the GetClass () method of object, which is suitable for situations where the type is unknown -Object obj =NewPerson (); -Clazz =Obj.getclass (); -System.out.println ("obtained by the GetClass method:" +clazz); in  -         //obtained by the full class name, using more, but will throw ClassNotFoundException exception toClazz = Class.forName ("Codeanal.person"); +System.out.println ("Get by the full class name:" +clazz); -  the         /* * * Create instantiated objects, get methods, invoke methods, get Properties, set properties $ * Note that exceptions need to be thrown herePanax Notoginseng          */ -         //get instantiated Object theObject object =clazz.newinstance (); +System.out.println ("instance object" +object); A         //getting methods in a class themethod = Clazz.getdeclaredmethod ("Run"); +System.out.println ("Method:" +method); -         //calling methods in a class $ Method.invoke (object); $         //Get Properties -Field field = Clazz.getdeclaredfield ("name"); -         //Setting Properties theField.setaccessible (true); -Field.set (object, "Zhang San");WuyiSystem.out.println ("Person Name:" +Field.get (object)); the  -     } Wu}

This article is just a brief introduction to the reflection mechanism in Java, for more information please refer to: http://www.cnblogs.com/Eason-S/p/5851078.html

                           

A brief analysis of reflection mechanism in Java

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.