Java Reflection Mechanism Learning essays

Source: Internet
Author: User

The Java reflection mechanism is used to dynamically generate classes at runtime information or methods, properties, can also be used for dynamic generation of classes, because all classes are subclasses of class, we can use a class class instance to instantiate a variety of classes

For example:

NULL ;          Classnull;                     // notation 1, may throw classnotfoundexception [multi-use this notation  ]        Class1 = Class.forName ("Cn.lee.demo.Person");          System.out.println ("Demo2: (notation 1) package name:" + class1.getpackage (). GetName () + ","                   + "full class Name:" + Class1.getname ( ));                     // notation 2          Class2 = person. class ;          System.out.println ("Demo2: (notation 2) package name:" + class2.getpackage (). GetName () + ","                   + "full class Name:" + class2.getname ());  

It's a common form.

There is another way to get internal methods/properties

NULL ;           = Class.forName ("Cn.lee.demo.Person");           = class1.newinstance ();                     = Class1.getdeclaredfield ("name");          Personnamefield.setaccessible (true);          " Fat Tiger First Sen ");                              System.out.println ("Demo5: Gets the value of the property variable after modifying the property:" + personnamefield.get (obj));

You can manipulate properties and methods inside a class

Java Reflection Mechanism Learning essays

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.