Java Reflection Learning (Java reflect) (ii)

Source: Internet
Author: User

Ok before said the reflection and reflection analysis of Java class, then these things have a horse effect, the following applications:

Iii. using reflection to analyze objects at run time

  • Simply write aEmployeeClass, then use java reflex to take name domain Getdeclarefield (string  name) can get all , even private above said, so to the 9< Span style= "FONT-FAMILY:SIMSUN; font-size:11pt, "lang=" ZH-CN "> OK.

,

But in the first11 Yes, it was wrong , because get (Field f) No access so illegalaccessexception, Here's a way Setaccessible (Boolean b), This cargo is for Debug, persistent storage, and similar mechanisms provide finish reading the following :

Access, of course, is easy to use Field.set (E, "hehe"); Such statements are assigned to a value ~

Iv. Reflection -- generic array

Array (java.lang.reflect) allows you to create arrays dynamically, following the implementation code:

  

But this code is object[]object[]string[],string[] turn into object[]

  

  Error:classcastexception:

  

So how is the conversion correct? , array There is a method called newinstance (class ) Span style= "FONT-FAMILY:CALIBRI;" lang= "en-US" ><?> componenttype, int length); This method you just given the array of the original type and the length of the array, then return come out objectstring[].

Maincode:

  

Method: (Arrayprint (int[]) is the print function , Goodarraygrow (int[] a) is the growth array function )

  

  

 Result:

 

Succeeded in increasing the array hohoho~

  

Java Reflection Learning (Java reflect) (ii)

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.