Java record-86-Reflection API example 2

Source: Internet
Author: User

Java record-86-Reflection API example 2
Obtain information under a specified class: all methods and attributes

Public class DumpClassInfo {public static void main (String [] args) throws Exception {// basic function of Reflection API Class <?> Classtype = Class. forName ("my. reflect. Customer"); Method [] methods = classtype. getDeclaredMethods (); // obtain all methods under the specified Class, including private methods. (All methods at runtime) for (Method method: methods) {System. out. println (method);} Field [] fields = classtype. getDeclaredFields (); // obtain all attributes of a specified class, including private attributes. (ALL runtime attributes) for (Field field: fields) {System. out. println (field) ;}}} class Customer {private long id; private String name; private int age; public Customer () {} public Customer (String name, int age) {this. name = name; this. age = age;} public long getId () {return id;} public void setId (long id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}

 

Output results of the above program:
public void my.reflect.Customer.setId(long)public int my.reflect.Customer.getAge()public void my.reflect.Customer.setAge(int)public java.lang.String my.reflect.Customer.getName()public long my.reflect.Customer.getId()public void my.reflect.Customer.setName(java.lang.String)private long my.reflect.Customer.idprivate java.lang.String my.reflect.Customer.nameprivate int my.reflect.Customer.age

 

 

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.