Class.forName(String fullyQualifiedClassName): It loads the class or interface at run time (dynamically) into the memory and returns the Class object associated with the class or interface with the given string name
- public class ClassForNameImpl {
- public static void main(String[] args) {
- try {
- Class<?> cls1 = Class.forName("com.ishaan.jdbc.LoadDemoClass");
- System.out.println("return value of forName() method: " + cls1);
- System.out.println("--------------------------------------");
- String qualifiedClassName = cls1.getName();
- System.out.println("return value of getName() method: " + qualifiedClassName);
- System.out.println("--------------------------------------");
- String onlyClassName = cls1.getSimpleName();
- System.out.println("return value of getSimpleName() method: " + onlyClassName);
- System.out.println("--------------------------------------");
- ClassLoader cLoader = cls1.getClassLoader();
- System.out.println("return value of getClassLoader() method: " + cLoader);
- System.out.println("--------------------------------------");
- System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
- System.out.println("--------------------------------------");
- System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName));
- System.out.println("#########################################");
- Class<?> cls2 = Class.forName("java.util.ArrayList");
- System.out.println("return value of forName() method: " + cls2);
- System.out.println("--------------------------------------");
- String qualifiedClassName2 = cls2.getName();
- System.out.println("return value of getName() method: " + qualifiedClassName2);
- System.out.println("--------------------------------------");
- String onlyClassName2 = cls2.getSimpleName();
- System.out.println("return value of getSimpleName() method: " + onlyClassName2);
- System.out.println("--------------------------------------");
- ClassLoader cLoader2 = cls2.getClassLoader();
- System.out.println("return value of getClassLoader() method: " + cLoader2);
- System.out.println("--------------------------------------");
- System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
- System.out.println("--------------------------------------");
- System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName2));
- System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
- Class<?> cls3 = Class.forName("java.lang.Thread");
- System.out.println("return value of forName() method: " + cls3);
- System.out.println("--------------------------------------");
- String qualifiedClassName3 = cls3.getName();
- System.out.println("return value of getName() method: " + qualifiedClassName3);
- System.out.println("--------------------------------------");
- String onlyClassName3 = cls3.getSimpleName();
- System.out.println("return value of getSimpleName() method: " + onlyClassName3);
- System.out.println("--------------------------------------");
- ClassLoader cLoader3 = cls3.getClassLoader();
- System.out.println("return value of getClassLoader() method: " + cLoader3);
- System.out.println("--------------------------------------");
- System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
- System.out.println("--------------------------------------");
- System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName3));
- } catch (Exception e) {
- System.out.println("Class not found, please pass the fully qualified class name as an input argument for the method Class.forName() !!");
- System.out.println(e.getMessage())
- }
- }
- }
return value of forName() method: class com.ishaan.jdbc.LoadDemoClass
--------------------------------------
return value of getName() method: com.ishaan.jdbc.LoadDemoClass
--------------------------------------
return value of getSimpleName() method: LoadDemoClass
--------------------------------------
return value of getClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of getSystemClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of loadClass() method: class com.ishaan.jdbc.LoadDemoClass
#########################################
return value of forName() method: class java.util.ArrayList
--------------------------------------
return value of getName() method: java.util.ArrayList
--------------------------------------
return value of getSimpleName() method: ArrayList
--------------------------------------
return value of getClassLoader() method: null
--------------------------------------
return value of getSystemClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of loadClass() method: class java.util.ArrayList
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
return value of forName() method: class java.lang.Thread
--------------------------------------
return value of getName() method: java.lang.Thread
--------------------------------------
return value of getSimpleName() method: Thread
--------------------------------------
return value of getClassLoader() method: null
--------------------------------------
return value of getSystemClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of loadClass() method: class java.lang.Thread
No comments:
Post a Comment