The 'Class' class also brings the possibility of creating an object without using the new keyword. : Class Object « Class Definition « Java Tutorial






  1. The static forName method creates a Class object of the given class name.
  2. The newInstance method creates a new instance of a class.
public class MainClass {
  public static void main(String[] a) {
    Class klass = null;
    try {
      klass = Class.forName("java.lang.String");
    } catch (ClassNotFoundException e) {
    
    }
    
    if (klass != null) {
      try {
        // create an instance of the Test class
        String test = (String) klass.newInstance();


      } catch (IllegalAccessException e) {
      } catch (InstantiationException e) {
      }

    }
  }
}








5.17.Class Object
5.17.1.Getting java.lang.Class: Information about your object
5.17.2.Comparing Objects
5.17.3.Class Object
5.17.4.Storing a reference to the String object as type Object
5.17.5.The 'Class' class also brings the possibility of creating an object without using the new keyword.
5.17.6.Assignment with objects is a bit tricky.
5.17.7.Demonstrate Run-Time Type Information.