- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Class forName() Method
Description
The Java Class forName(String name, boolean initialize, ClassLoader loader) method returns the Class object associated with the class or interface with the given string name, using the given class loader.
The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.
Declaration
Following is the declaration for java.lang.Class.forName() method
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 shows whether the class must be initialized.
loader − This is the class loader from which the class must be loaded.
Return Value
This method returns the class object representing the desired class.
Exception
LinkageError − if the linkage fails.
ExceptionInInitializerError − if the initialization provoked by this method fails.
ClassNotFoundException − if the class cannot be located by the specified class loader.
Getting Class by Name and a Given Class Loader Example
The following example shows the usage of java.lang.Class.forName() method. Using forName() method, we've received the Class of ClassDemo class by its name. Then using getClassLoader, class loader is retrieved. With the classLoader, Class of Thread is retrieved and printed.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { try { Class cls = Class.forName("com.tutorialspoint.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()); } } }
Output
Let us compile and run the above program, this will produce the following result −
Class = com.tutorialspoint.ClassDemo Class = java.lang.Thread
Getting Class by Name and a Thread Class Loader Example
The following example shows the usage of java.lang.Class.forName() method. Using forName() method, we've received the Class of ClassDemo class by its name. Then using Thread.class.getClassLoader(), class loader is retrieved. With the classLoader, Class of Thread is retrieved and printed.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { try { Class cls = Class.forName("com.tutorialspoint.ClassDemo"); /* 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, Thread.class.getClassLoader()); // 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()); } } }
Output
Let us compile and run the above program, this will produce the following result −
Class = com.tutorialspoint.ClassDemo Class = java.lang.Thread
To Continue Learning Please Login
Login with Google