Viewing class information by reflection

Source: Internet
Author: User

Viewing class information by reflection

1. Three ways to get a class object

(1) Use the class forname (String clazzname) static method. The method needs to pass in the string parameter, which is the fully qualified name of a class (the full package name must be added);

// For example, loading a database driver in JDBC Class.forName ("Com.mysql.jdbc.Driver");

(2) Call the class property of a certain classes to get the class object of the classes;

// assuming that the existing student class // call the Class property of the classes to get the class object Class clazz = Student. class;

(3) Call the GetClass () method of an object. This method is a method in Java.lang.Object, so all objects can use this method, which returns the class object to which the object belongs.

// Create a Student object New Student (); // use the object's GetClass () method to get the class object for that object Class clazz = S.getclass ();

2. Get the constructors contained in class corresponding classes

(1) constructor<t> GetConstructor (class<?>...parametertype):

Returns the public constructor with the specified formal parameter list for the class of this class object;

(2) constructor<t>[] GetConstructor ():

Returns all the public constructors for the class of this class object;

(3) constructor<t> Getdeclaredconstructor (class<?>...parametertype):

Returns a constructor independent of the access permission for the class of this class object, with the specified parameter list;

(4) constructor<t>[] Getdeclaredconstructor ():

Returns all constructors for the class of this class object, regardless of access permissions.

Viewing class information by reflection

Related Article

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.