Describe
The Java.lang.Class.forName (string name, Boolean initialize, ClassLoader loader) method returns a class object with the given string name, Use the given class loader.
The class or interface that is loaded by the specified class loader. If the parameter loader loader is empty, the class is loaded by booting the class loader. When the initialization of the class is initialized, the initialize parameter is true if it is not initialized.
Statement
The following is a declaration of the Java.lang.Class.forName () method
[Java]View Plain copy
- Public static class<?> forname (String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException
Parameters
name -This is the fully qualified name of the desired class.
Initialize -This indicates whether the class must be initialized.
Loader -This is the class loader for classes that must be loaded.
return value
This method returns the class object of the desired class.
Abnormal
Linkageerror --if linkage fails.
Exceptionininitializererror --if the initialization caused by this method fails.
classnotfoundexception --If the class cannot be located by the specified class loader.
Instance
The following example shows how to use the Java.lang.Class.forName () method.
[Java]View Plain copy
- Import java.lang.*;
- Public class Classdemo {
- public static void Main (string[] args) {
- try {
- Class cls = Class.forName ("Classdemo");
- //Returns the ClassLoader object
- ClassLoader Cloader = Cls.getclassloader ();
- / * Returns the class object associated with the class or interface
- With the given string name, using the given ClassLoader. */
- Class cls2 = Class.forName ("Java.lang.Thread", true, Cloader);
- //Returns the name of the class
- System.out.println ("Class =" + Cls.getname ());
- System.out.println ("Class =" + Cls2.getname ());
- }
- catch (ClassNotFoundException ex) {
- System.out.println (Ex.tostring ());
- }
- }
- }
Let's compile and run the above program, which will produce the following results:
[Java]View Plain copy
- Class = Classdemo
- Class = Java.lang.Thread
Java.lang.Class.forName (String name, Boolean initialize, ClassLoader loader) method