Annotations and Reflections
class class
The following method is redefined in the Object class, which will be inherited by all subclasses
public final Class getClass()
The type of the return value of the above method is a Class class, which is the source of Java reflection. In fact, the so-called reflection is also well understood from the running results of the program, that is, the name of the class can be obtained through object reflection.
The information that the object can get after looking in the mirror: the properties, methods and constructors of a certain class, and which interfaces a certain class implements. For each class, the JRE especially reserves an unchanging Class attribute. A Class object contains information about a particular structure.
(1) Class itself is also a class
(2) Class objects can only have objects created by the system
(3) A loaded class will only have one Class instance in the JVM
(4) A Class object corresponds to a class loaded into the JVM A .class file
(5) Each class instance will remember that it was generated by that Class instance
(6) Through Class, all loaded structures in a class can be obtained completely
(7) Class class is the root of Reflection , for any class you want to dynamically load and run, only the corresponding Class object in stock
Common methods of the Class class
method name | Function Description |
---|---|
static ClassforName(String name) | Returns the Class object of the specified class name name |
Obect newInstance() | Call the default constructor, returning an instance of the Class object |
getName() | Returns the name of the entity (class, interface, array class, or void) represented by this Class object |
Class getSuperClass() | Returns the Class object of the parent class of the current Class object |
Class[] getinterfaces() | Returns the interface of the current Class object |
ClassLoader getClassLoader() | Returns the class loader for this class |
ClassLoader[] getConstructors() | Returns an array containing some Constructor objects |
Method getMethod(String name,Class… T) | Returns a Method object whose parameter type is paramType |
Field[] getDeclaredFields() | Returns an array of Field objects |
Get an instance of the Class class
(1) The specific class is known and obtained through the class attribute of the class. This method is the safest and most reliable.
Class c1 = Person.class
(2) For an instance of a certain class, call the getClass() method of the instance to obtain the Class object
Class c1 = person.getClass();
(3) The full class name of a class is known, and the class is in the class path, which can be obtained through the static method forName() of the Class class, and ClassNotFoundException may be thrown
Class c1 = Class.forName("demo.Student");
Which types can have Class objects
(1) class: external class, member (member internal class, static internal class), local internal class, anonymous internal class
(2) inferfance: interface
(3) []: array (4
) enum: enumeration
(5) annotation : annotation @interfance
(6) primitive type: basic data type
(7) void