Several methods for obtaining Class objects in Java

Source: Internet
Author: User

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

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.