You can get the Class reference of any class during run time through the Java Reflection Concept.
Check the Below Code. Explanation is given below
Here is one example that uses returned Class to create an instance of AClass:
package com.xyzws;
class AClass {
public AClass() {
System.out.println("AClass's Constructor");
}
static {
System.out.println("static block in AClass");
}
}
public class Program {
public static void main(String[] args) {
try {
System.out.println("The first time calls forName:");
Class c = Class.forName("com.xyzws.AClass");
AClass a = (AClass)c.newInstance();
System.out.println("The second time calls forName:");
Class c1 = Class.forName("com.xyzws.AClass");
} catch (ClassNotFoundException e) {
// ...
} catch (InstantiationException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
}
}
The printed output is
The first time calls forName:
static block in AClass
AClass's Constructor
The second time calls forName:
The class has already been loaded so there is no second "static block in AClass"
The Explanation is below
Class.ForName is called to get a Class Object
By Using the Class Object we are creating the new instance of the Class.
Any doubts about this let me know