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