The second piece of reflection in Java

Source: Internet
Author: User

Reflection in JavaI. Overview of Reflections

The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, 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. To dissect a class, you must first obtain the bytecode file object for that class.

The anatomy uses the method in class. So first get the object of class type corresponding to each bytecode file.

1. Create an entity class

1  Public classUser {2     Private intID;3     Private intAge ;4     PrivateString uname;5     6      PublicUser (intIdintAge , String uname) {7         Super();8          This. ID =ID;9          This. Age =Age ;Ten          This. uname =uname; One     } A      PublicUser () { -         Super(); -         //TODO auto-generated Constructor stub the     } -      Public intgetId () { -         returnID; -     } +      Public voidSetId (intID) { -          This. ID =ID; +     } A      Public intGetage () { at         returnAge ; -     } -      Public voidSetage (intAge ) { -          This. Age =Age ; -     } -      PublicString Getuname () { in         returnuname; -     } to      Public voidsetuname (String uname) { +          This. uname =uname; -     } the      * @Override $      PublicString toString () {Panax Notoginseng         return"User [id=" + ID + ", age=" + Age + ", uname=" + uname + "]"; -     } the      +}

2, test the Java.lang.Class object acquisition method

1 /**2 * Acquisition of different types of class objects3 * Test how Java.lang.Class objects are obtained4  * @authorZhang Xiaodao5  *6  */7  Public classDemo01 {8      Public Static voidMain (string[] args) {9String Path = "Com.zzp.bean.User"; Ten         Try { One             //load JavaBean AClass Clazz =class.forname (path); -              -             //The object is to represent or encapsulate some data, a class is loaded (only once), and the JVM creates a class object that should be the             //The entire structure information of the class is placed in the corresponding class object, which is like a mirror -             //through this mirror, we can see all the information of the corresponding class. - System.out.println (clazz); -             //A class corresponds to only one class object and is loaded only once +Class CLAZZ2 =class.forname (path); -              +             //get objects by. Class and GetClass () AClass clazz3 =user.class; atClass clazz4 =Path.getclass (); -              -             //basic data types are also available through. Class -Class int1 =int.class; -              -             //can also get the array object, one-dimensional array Arr01 and Arr02 are the same, the two-dimensional array arr03 is different from the previous two, in             //The type of the array is different , and the object obtained is not the same. D is not the same as the previous three -             int[] Arr01 =New int[10]; to             int[] Arr02 =New int[30]; +             int[] Arr03 =New int[10] [20]; -             Double[] D =New Double[10]; the                      *}Catch(ClassNotFoundException e) { $             //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); -         } the     } +}

The second piece of reflection 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.