Summary of Java Reflection

Source: Internet
Author: User

One. 3 ways to get a class object:

1. Class.forName (""); for example: Class.forName ("java.lang.String");

2. Class.class; for example: String.class; This method requires a class

3. Object. GetClass (); For example: string str = new string (); Str.getclass; This method requires an object

Two. Reflection Generation Object Method:

1. Create the Class object first, and generate the object directly from the Newinstance () method of the Class object. The object generated by this method can only be constructed without parameters;

class<?> CLZ = String.class; Object obj = clz.newinstance ();

2. First create the Class object, in the Get object constructor object, in passing the constructor object's Newinstance () method;

class<?> CLZ = Customer.class;

Constructor con = clz.getdeclaredconstructor (new class[] {String. Class, Int.class});

Object obj = con.newinstance (new object[]{"Hello", "Ten}");

Summary of 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.