The forName(String, boolean, ClassLoader) method of java.lang.Class class is used to get the instance of this Class with the specified class name, using the specified class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.
Syntax:
public static Class<T>
forName(String className,
boolean initialize,
ClassLoader classLoader)
throws ClassNotFoundException
Parameter: This method accepts following parameters:
- className which is the Class for which its instance is required.
- initialize which is the boolean to state if this class instance needs to be initialized or not.
- classLoader which is the ClassLoader from which this Class must be loaded.
Return Value: This method returns the instance of this Class fetched using the specified parameters.
Exception: This method throws following Excpetions:
- LinkageError: if the linkage fails
- ExceptionInInitializerError: if the initialization provoked by this method fails
- ClassNotFoundException: if the class cannot be located
- SecurityException: if a security manager is present, and the loader is null, and the caller’s class loader is not null, and the caller does not have the RuntimePermission(“getClassLoader”)
Below programs demonstrate the forName() method.
Example 1:
// Java program to demonstrate forName() method public class Test { public static void main(String[] args) throws ClassNotFoundException { // returns the Class object for this class Class myClass = Class.forName("Test"); ClassLoader loader = myClass.getClassLoader(); // get the Class instance using forName method Class c1 = Class.forName("java.lang.String", true, loader); System.out.print("Class represented by c1: " + c1.toString()); } } |
Class represented by c1: class java.lang.String
Example 2:
// Java program to demonstrate forName() method public class Test { public static void main(String[] args) throws ClassNotFoundException { // returns the Class object for this class Class myClass = Class.forName("Test"); ClassLoader loader = myClass.getClassLoader(); // get the Class instance using forName method Class c1 = Class.forName("java.lang.Integer", false, loader); System.out.print("Class represented by c1: " + c1.toString()); } } |
Class represented by c1: class java.lang.Integer
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Guava | Booleans.indexOf(boolean[] array, boolean[] target) method with Examples
- Java Guava | Booleans.indexOf(boolean[] array, boolean target) method with Examples
- Boolean toString() method in Java with examples
- Boolean parseBoolean() method in Java with examples
- Boolean compareTo() method in Java with examples
- Boolean booleanValue() method in Java with examples
- Boolean compare() method in Java with Examples
- Boolean hashCode() method in Java with examples
- Boolean equals() method in Java with examples
- PrintStream println(boolean) method in Java with Examples
- PrintWriter print(boolean) method in Java with Examples
- TimeZone getDisplayName(boolean, int) Method in Java with Examples
- PrintStream print(boolean) method in Java with Examples
- Matcher useAnchoringBounds(boolean) method in Java with Examples
- PrintWriter println(boolean) method in Java with Examples
- Matcher useTransparentBounds(boolean) method in Java with Examples
- ChoiceFormat nextDouble(double, boolean) method in Java with Examples
- ClassLoader in Java
- java.lang.Boolean class methods
- Java.lang.Boolean Class in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

