Several methods for obtaining Class objects in Java
1. class, enumeration, interface, annotation, array type, native type name. class
Example:
Class classString = String. class; // Class class classEnum = RetentionPolicy. class; // enumerate Class classInterface = Serializable. class; // Interface Class classAnnotation = Retention. class; // annotation Class classInt = int. class; // native Class classIntArray = int []. class; // native array type Class classStringArray = String []. class; // array type
2. Object. getClass ()
Because the native type is not an object, getClass () cannot be used. Other types are supported.
Example
Class classString = new String (). getClass (); // Class classEnum = RetentionPolicy. SOURCE. getClass (); // enumerate Class classInterface = new Serializable (){}. getClass (); // Interface Class classAnnotation = new incluented () {public Class
AnnotationType () {return null ;}}. getClass (); // annotation // Class classInt = ...; // The native type is not an object. You cannot use the getClass () method Class classIntArray = new int [] {}. getClass (); // native array type Class classStringArray = new String [] {}. getClass (); // array type
3. Use Class. forName
The Class. forName method has two methods:
1. forName (String name)
2. forName (String name, boolean initialize, ClassLoader loader)
ForName (String name) actually calls forName (String name, boolean initialize, ClassLoader loader)
forName(className, true, ClassLoader.getCallerClassLoader());
The boolean initialize parameter is critical. If it is true, the class will be initialized, the static variable will be assigned the initial value, the static code block will be executed, and if it is false, it will not be initialized.
Class. forName still does not support native types, but other types are supported.
Example:
Class classString = Class. forName ("java. lang. string "); // Class classEnum = Class. forName ("java. lang. annotation. retentionPolicy "); // enumerate Class classInterface = Class. forName ("java. io. serializable "); // Interface Class classAnnotation = Class. forName ("java. lang. annotation. documented "); // annotation // Class classInt = ...; // The native type is not an object and Class cannot be used. forName method Class classIntArray = Class. forName ("[I"); // native array type Class classStringArray = Class. forName ("[Ljava. lang. string; "); // array type
4. Use ClassLoader. loadClass
This method can also load classes, with the same effect as Class. forName (className, false, ClassLoader. getCallerClassLoader (), without Class initialization.
However, compared with Class. forName, ClassLoader. loadClass cannot be used for array types.
Apart from native and array types, other types are supported.
Example:
Class classString = ClassLoader. getSystemClassLoader (). loadClass ("java. lang. string "); // Class classEnum = ClassLoader. getSystemClassLoader (). loadClass ("java. lang. annotation. retentionPolicy "); // enumeration Class classInterface = ClassLoader. getSystemClassLoader (). loadClass ("java. io. serializable "); // Interface Class classAnnotation = ClassLoader. getSystemClassLoader (). loadClass ("java. lang. annotation. documented ");// Annotation // Class classInt = ...; // The native type is not an object and ClassLoader cannot be used. loadClass method // Class classIntArray = ClassLoader. getSystemClassLoader (). loadClass ("[I"); // ClassLoader cannot be used for array types. loadClass method // Class classStringArray = ClassLoader. getSystemClassLoader (). loadClass ("[Ljava. lang. string; "); // ClassLoader cannot be used for array types. loadClass Method