Sunday, April 29, 2018

java.lang.Class.forName() method features !!

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

  1. public class ClassForNameImpl {
  2. public static void main(String[] args) {
  3. try {
  4. Class<?> cls1 = Class.forName("com.ishaan.jdbc.LoadDemoClass");
  5. System.out.println("return value of forName() method: " + cls1);
  6. System.out.println("--------------------------------------");
  7. String qualifiedClassName = cls1.getName();
  8. System.out.println("return value of getName() method: " + qualifiedClassName);

  9. System.out.println("--------------------------------------");
  10. String onlyClassName = cls1.getSimpleName();
  11. System.out.println("return value of getSimpleName() method: " + onlyClassName);

  12. System.out.println("--------------------------------------");
  13. ClassLoader cLoader = cls1.getClassLoader();
  14. System.out.println("return value of getClassLoader() method: " + cLoader);

  15. System.out.println("--------------------------------------");
  16. System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
  17. System.out.println("--------------------------------------");
  18. System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName));

  19. System.out.println("#########################################");

  20. Class<?> cls2 = Class.forName("java.util.ArrayList");
  21. System.out.println("return value of forName() method: " + cls2);

  22. System.out.println("--------------------------------------");
  23. String qualifiedClassName2 = cls2.getName();
  24. System.out.println("return value of getName() method: " + qualifiedClassName2);

  25. System.out.println("--------------------------------------");
  26. String onlyClassName2 = cls2.getSimpleName();
  27. System.out.println("return value of getSimpleName() method: " + onlyClassName2);

  28. System.out.println("--------------------------------------");
  29. ClassLoader cLoader2 = cls2.getClassLoader();
  30. System.out.println("return value of getClassLoader() method: " + cLoader2);

  31. System.out.println("--------------------------------------");
  32. System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
  33. System.out.println("--------------------------------------");
  34. System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName2));

  35. System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

  36. Class<?> cls3 = Class.forName("java.lang.Thread");
  37. System.out.println("return value of forName() method: " + cls3);

  38. System.out.println("--------------------------------------");
  39. String qualifiedClassName3 = cls3.getName();
  40. System.out.println("return value of getName() method: " + qualifiedClassName3);

  41. System.out.println("--------------------------------------");
  42. String onlyClassName3 = cls3.getSimpleName();
  43. System.out.println("return value of getSimpleName() method: " + onlyClassName3);

  44. System.out.println("--------------------------------------");
  45. ClassLoader cLoader3 = cls3.getClassLoader();
  46. System.out.println("return value of getClassLoader() method: " + cLoader3);

  47. System.out.println("--------------------------------------");
  48. System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
  49. System.out.println("--------------------------------------");
  50. System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName3));

  51. } catch (Exception e) {
  52. System.out.println("Class not found, please pass the fully qualified class name as an input argument for the method Class.forName() !!");
  53. System.out.println(e.getMessage())
  54. }
  55. }
  56. }
Output: 

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

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...