1. class, enum, interface, annotation, array type, name of the native type. class
Example:
Classclassstring=String.class;//ClassClassClassenum=retentionpolicy.class;//EnumerationClassClassinterface=serializable.class;//InterfaceClassClassannotation=retention.class;//AnnotationsClassClassint=int.class;//Native typeClassClassintarray=int[].class;//native array typeClassclassstringarray=String[].class;//Array type
2. Object. GetClass ()
Because the native type is not an object, getclass () cannot be used, and other types are supported.
Example
Class classstring =NewString (). GetClass ();//ClassClass classenum = RetentionPolicy.SOURCE.getClass ();//EnumerationClass ClassInterface =NewSerializable () {}.getclass ();//InterfaceClass classannotation =NewDocumented () { Publicclass<? Extends annotation>Annotationtype() {return NULL;}}. GetClass ();//Annotations//Class classint= ... ;//native type is not an object, cannot use the GetClass () methodClass Classintarray =New int[] {}.getclass ();//native array typeClass Classstringarray =NewString[] {}.getclass ();//Array type
3. Using Class.forName
There are two methods of Class.forName:
1.forName (String name)
2.forName (String name, Boolean Initialize,classloader loader)
forname (string name) is actually called forname (string Name,boolean initialize,classloader loader)
fortrue, ClassLoader.getCallerClassLoader());
The Boolean initialize parameter is critical, and if true, the class is initialized, the static variable is assigned an initial value, the static code block is executed, and if false it is not initialized.
Class.forName still does not support native types, but other types are supported.
Example:
class classstring = Class.forName ( " Java.lang.String "); //class Class classenum = class.forname (); //enumeration Class classinterface = class.forname ( " Java.io.Serializable "); //interface Class classannotation = class.forname (); //annotation //class classint= ... ;//The native type is not an object, you cannot use the Class.forName method Class Classintarray = class.forname ( "[I" ); //native array type Class Classstringarray = class.forname ( "[Ljava.lang.String;" ); //array type
4. Using Classloader.loadclass
This method can also load a class, with an effect of Class.forName (ClassName, False, Classloader.getcallerclassloader ()), which does not initialize the class.
But Classloader.loadclass compared to class.forname, Classloader.loadclass cannot be used on array types.
In addition to native types and array types, other types are supported.
Example:
Class classstring = ClassLoader. Getsystemclassloader(). LoadClass("Java.lang.String");//ClassClass Classenum = ClassLoader. Getsystemclassloader(). LoadClass("Java.lang.annotation.RetentionPolicy");//EnumerationClass ClassInterface = ClassLoader. Getsystemclassloader(). LoadClass("Java.io.Serializable");//InterfaceClass classannotation = ClassLoader. Getsystemclassloader(). LoadClass("java.lang.annotation.Documented");//AnnotationsClass classint= ...;//native type is not an object, cannot use the Classloader.loadclass methodClass Classintarray = ClassLoader. Getsystemclassloader(). LoadClass("[I");//array type cannot use the Classloader.loadclass methodClass Classstringarray = ClassLoader. Getsystemclassloader(). LoadClass("[Ljava.lang.String;]);//array type cannot use the Classloader.loadclass method
Several ways in which Java obtains class objects