Several ways in which Java obtains class objects

Source: Internet
Author: User

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

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.