Different ways to create Objects in Java



Following are the different ways to create objects in Java.

  • Using new keyword − Mostly used method. Call any constructor with new keyword to create an object.

    Tester t = new Tester();
  • Using Class.forName().newInstance() − Load the class using Class.forName() and then call its newInstance() method to create the object.

    Tester t = Class.forName("Tester").newInstance();
  • Using clone() method − Get the clone object of the required object by calling its clone() method.

    Tester t = new Tester();
    Tester t1 = t.clone();
  • Using Deserialization − JVM creates a new object when it is de serialized.

    Tester t = new Tester();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(t);
    objectOutputStream.flush();
    objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
    Tester t1= objectInputStream.readObject();
  • Using Reflection − Using Constructor.newInstance() method, we can create a new object.

    Constructor<Tester> constructor = Tester.class.getDeclaredConstructor();
    Tester r = constructor.newInstance();

Example

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Tester implements Serializable, Cloneable {
   protected Object clone() throws CloneNotSupportedException {
      return super.clone();
   }
   public static void main(String args[])
   throws InstantiationException, IllegalAccessException
   , ClassNotFoundException, CloneNotSupportedException
   , IOException, NoSuchMethodException, SecurityException
   , IllegalArgumentException, InvocationTargetException {

      //Scenario 1: using new keyword
      Tester t = new Tester();
      System.out.println(t);

      //Scenario 2: using Class.forName().newInstance()
      Tester t1 = (Tester) Class.forName("Tester").newInstance();
      System.out.println(t1);

      //Scenario 3: using clone() method
      Tester t3 = (Tester) t.clone();
      System.out.println(t3);

      //Scenario 4: using Deserialization method

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
      objectOutputStream.writeObject(t);
      objectOutputStream.flush();
      ObjectInputStream objectInputStream = new ObjectInputStream(
      new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
      Tester t4 = (Tester) objectInputStream.readObject();
      System.out.println(t4);

      //Scenario 5: using Reflection method
      Constructor<Tester> constructor = Tester.class.getDeclaredConstructor();
      Tester t5 = constructor.newInstance();
      System.out.println(t5);
   }
}

Output

Tester@2a139a55
Tester@15db9742
Tester@6d06d69c
Tester@55f96302
Tester@3d4eac69

Advertisements