Creating object without new Keyword

Creating an object of a Class without using new Keyword

It is of general practice to create an object with new keyword. It is also possible to create an object without new keyword. This is of logical significance only and not used in regular practice.

public class Hello
  public void display()
    System.out.println("Hello World");
  public static void main(String args[]) throws Exception
    Class c1 = Class.forName("Hello");  // throws ClassNotFoundException
    Object obj1 = c1.newInstance( );    // throws InstantiationException and 
			                // IllegalAccessException
    Hello h1 = (Hello) obj1;

Creating object without new Keyword
Output screenshot of Creating object without new Keyword

Class c1 = Class.forName(“Hello”);
Object obj1 = c1.newInstance( );

forName() is a static method of java.lang.Class that loads the .class file of Hello into the RAM and returns the Hello as an object of class Class. c1 contains the reference of Hello. newInstance() method of class Class returns an object of Object class. Now, obj1 contains the reference of Hello. Internally, JVM may use new keyword.

Hello h1 = (Hello) obj1;

The object obj1 is explicitly type casted to Hello object, h1. It is a long process.

Total there are 5 styles of creating an object in Java without new keyword. This is one. Would you like to know the other 4 styles also.

13 thoughts on “Creating object without new Keyword”

  1. Purna Pattanayak

    in the above context why we need to perform explicit type casting as the obj1 contains reference of hello???why we again explicitly type cast it to hello???plz explain???

  2. hi,
    i have understood that how the implicit casting works.
    but, i am confused with the explicit casting. can you please explaing?
    for eg:
    A is a super class and B is a subclass and class B contains the mettod b1. Please see below:
    A a=new B();
    a.b1();// why does this statement throws an error?
    and if i do like:
    B b=(B)a;
    a.b1();// why this statement doesn’t throw the error?

  3. hello

    i have written this code

    package exp;
    import java.lang.*;
    public class F1
    static int power=100;

    public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException
    System.out.println(“Hello World”);
    System.out.println(“Best wishes”);
    // System.out.println(“Speed of the car is “+power+”Kmph”);
    Class c1=Class.forName(“F1”);
    Object ob1=c1.newInstance();
    F1 ferrari=(F1)ob1;
    System.out.println(“speed of ferrari is”+ferrari.power+”Kmph”);


    i am not getting the output .can u please let me know where i went wrong

          1. Hi Vinay/Prathak,

            Probably you are not giving the fully qualified name of the class(I mean, complete class name with package) in Class.forName(…) method. Check once.


    1. if you are using eclips then you should write full package name
      class Example{
      public void display(){
      System.out.println(“hello java”);
      public static void main(String args[])throws Exception{
      Class c= Class.forName(“com.nt.beans.Example);
      Object obj=c.newInstance();
      Example e=(Example)obj;
      output : hello java

Leave a Comment

Your email address will not be published.