Java Reflection Knowledge

Source: Internet
Author: User

The Java reflection mechanism is in the running state, and for any class file, you 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.
Dynamic access to the information in the class is the Java reflection

The role of Java reflection (you can use the defined code to extend the configuration file, without modifying the source code, the source file provides an interface, the developer can write the implementation of the interface class, the class has processing requests and responses, and then configured to achieve the extension of the original program)

Package Reflect.test;import Java.lang.reflect.constructor;import Java.lang.reflect.field;import Java.lang.reflect.invocationtargetexception;import Useclass. Person;public class Reflectdemo {public static void main (string[] args) throws ClassNotFoundException, Instantiationexception, Illegalaccessexception, SecurityException, IllegalArgumentException, NoSuchMethodException , InvocationTargetException, nosuchfieldexception {Getfielddemo ();//Get field creatnewclass ();// Create an object based on the class file Creatnewclass_2 ();//The constructor method with parameters is instantiated}public static void Getfielddemo () throws ClassNotFoundException, SecurityException, Nosuchfieldexception, IllegalArgumentException, Illegalaccessexception, InstantiationException { Class cla=class.forname ("Useclass. Person "); Field F=null;//cla.getfield ("Age"); F=cla.getdeclaredfield ("name");//access to private fields Cancel permission check, brute force access f.setaccessible (true); O Bject obj=cla.newinstance (); F.set (obj, "Xiao Qiang");//Set the name to cockroach object O=f.get (obj); System.out.println (o);} public static void Creatnewclass_2 () throws ClassnotfoundexcePtion, SecurityException, Nosuchmethodexception, IllegalArgumentException, Instantiationexception, Illegalaccessexception, InvocationTargetException {/* * When getting the object that is embodied in the specified name corresponding to the class, * and the object initialization does not use NULL argument constructs, you should first get to the constructor * A byte-code file object can be completed * method is: GetConstructor (paramtertypes); */string name= "Useclass. Person "; Class Cla=class.forname (name);//Get class file constructor Con=cla.getconstructor (string.class,int.class);// Gets the specified argument constructor object Obj=con.newinstance ("Xiao Qiang", 32);//Instantiate System.out.println with the specified parameter constructor (Cla.getfields ());} public static void Creatnewclass () throws ClassNotFoundException, Instantiationexception, illegalaccessexception{// Early: When new, the class's bytecode is searched for by the name of the new class, loaded into memory,//and the bytecode file object is created, and then the corresponding person object of the byte file is created Useclass.person p=new useclass. Person ();//Now: String name= "Useclass. Person ";//Search for the name class file and load into memory, and produce class object class Cla=class.forname (name);//The object that produces the class objects obj=cla.newinstance ();// Gets the constructor with parameters and instantiates}}

Java Reflection Knowledge

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.