Java reflection comprehension and Java reflection Comprehension

Source: Internet
Author: User

Java reflection comprehension and Java reflection Comprehension
Reflection: Get the class when the object is running.
Person. class --> Person object Class. class --> Class Object
We create a Person object through the Person. class bytecode file to represent a real Person. The Person object is used to map a Person. It is not a bytecode file on the hard disk, but a Person object created based on the loading class in the memory. The loaded Class is actually a Class object. It only contains static members.
We use Class. the class bytecode file creates a Class object that represents a Class in reality. The class object is used to map a Class. It is not a bytecode file on the hard disk, instead, the bytecode information is encapsulated into an object in the memory, which is a Class object. This object maps to a class.
------------------------------------------------------------------
The completion process of Person p = new Person () is as follows: first find the Person. class bytecode file on the hard disk. Then, the bytecode information is encapsulated into a Class object in the memory. The object type is a Class object, referred to as the loading Class, and then a Person object is created through the Class object.

Note: Each object (NEW object) corresponds to a Class Object (loading CLass). Different Class objects represent data types not used in JAVA. All classes in JAVA are encapsulated into class objects.

Public static void main (String [] args) throws Exception {// Person. class --> Person object --> zhangsan // Class. class --> Class Object (indicating a type) --> Person. classObject obj = new Person ("zhangsan", 19); // There are three methods to obtain the object type/* 1. if the object Class clazz = obj is obtained. getClass (); // returns an Instance Object of the class type. String classname = clazz. getName (); System. out. println (classname); * // * 2. if the String classname = "cn. itcast. day9.reflectstudy. person "; Class clazz = Class. forName (classname); System. out. println (clazz. getName (); * // * 3. clearly know which type of Class Object To take Class clazz = Person. class; System. out. println (clazz. getName (); System. out. println (int. class. getName (); */Class clazz = obj. getClass ();/* traverse all attributes of the class Field [] fields = clazz. getDeclaredFields (); for (int I = 0; I <fields. length; I ++) {String name = fields [I]. getName (); Class type = fields [I]. getType (); System. out. println ("property name:" + name + "property type:" + type. getName ();} * // obtain an attribute and access this attribute Field field = clazz. getDeclaredField ("name"); field. setAccessible (true); // The access check field is canceled. set (obj, "Zeng ge"); System. out. println (obj); Object value = field. get (obj); System. out. println (value); * // * obtain the run Method of the object, and call Method m = clazz. getDeclaredMethod ("run", null); m. invoke (obj, null); * // * obtain the class Constructor construction method, and create the class instance */Constructor con = clazz. getConstructor (String. class, int. class); Object object = con. newInstance ("zhangsan", 29); Person p = (Person) object; System. out. println (p );}


Related Article

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.