JAVA---Reflection

Source: Internet
Author: User

When a class object is used, the JVM loads the class bytecode file into memory (one time only), and the JVM encapsulates the information of this class into objects. The use of encapsulated objects to obtain information about the class to construct classes or call methods, etc., is called reflection.

The purpose and function of reflection is to write more generic code for developers.

    • Encapsulate class names, inheritance, and implementations into class objects
    • Encapsulates a class's construction method into a constructor object
    • Encapsulates the member properties of a class into a file object
    • Encapsulates the normal method and return value of a class into a method object

Example

 PackageCom.myjava.reflect; Public classperson{Private int_age; PrivateString _name;  Public intGet_age () {return_age; }     Public voidsay () {System.out.println (_name + "this year" + _age + "years old");    }     Public voidSet_age (int_age) {         This. _age =_age; }     PublicPerson (int_age, String _name) {        Super();  This. _age =_age;  This. _name =_name; }     PublicString get_name () {return_name; }     Public voidset_name (String _name) { This. _name =_name; }}

Main program

 PackageCom.myjava.reflect;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;Importjava.lang.reflect.InvocationTargetException;/*** @ClassName: Reflecttest * @Description: TODO (here is a word describing the role of this class) *@authorLiu Yangyang * @date October 5, 2016 morning 9:20:32 **/ Public classreflecttest{ Public Static voidMain (string[] args)throwsclassnotfoundexception, Nosuchmethodexception, SecurityException, Illegalaccessexception, IllegalArgumentE Xception, InvocationTargetException, instantiationexception {//loaded according to the class name, this load does not build object constructsClass MyClass = Class.forName ("Com.myjava.reflect.Person"); //This needs to import the corresponding person//Class myClass = Person.class; //This is to get the corresponding class information after the instantiation.//Class myClass = new Person (one, "Honey"). GetClass ();System.err.println (Myclass.getname ());//Get class namefield[] fields = Myclass.getdeclaredfields ();//Get member Fields         for(Field f:fields) {System.err.println (F.getname ());//Get member nameSystem.err.println (F.getmodifiers ());//declared permissions default private public//protectedSystem.err.println (F.gettype ());//Get member Type} constructor[] Constructors=myclass.getdeclaredconstructors ();  for(Constructor con:constructors) {System.err.println (Con.getname ()); System.err.println (Con.getmodifiers ());//declared permissions default private//Public protectedSystem.err.println (Con.getparametercount ());//Number of parametersSystem.err.println (Con.getparameters ());//Parameter Collection        }        //call the Say method in this object to construct the parameter bits _age=11,_name=honeyMyclass.getmethod ("Say"). Invoke (Myclass.getconstructor (int.class, String.class). newinstance (One, "Honey"),NULL); }}

The result of the program execution is

Com.myjava.reflect.Person
_age
2
Int
_name
2
Class Java.lang.String
Com.myjava.reflect.Person
1
2
[ljava.lang.reflect.parameter;@15db9742
Honey is 11 years old this year.

JAVA---Reflection

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.