Tell me different ways to create an object in Java?

Sumbal Samoon
Sumbal Samoon

Posted On: Dec 18, 2020


The object is a real world entity which has state, behavior and identity. You can say that object is an instance of a class.

Different ways to create an object in Java are as follows:

Using new keyword:

ClassName obj1 = new ClassName();

Using predefined Class class’s newInstance() method:

ClassName obj2 = ClassName.class.newInstance();

Using Constructor class’s newInstance() method:

Constructor constructor = ClassName.class.getConstructor();

ClassName obj3 = constructor.newInstance();

Using clone() method:

ClassName obj4 = (ClassName) obj3.clone();

Using deserialization:

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(“data.obj”));


ObjectInputStream in = new ObjectInputStream(new FileInputStream(“data.obj”));

ClassName obj5 = (ClassName) in.readObject();

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    HCL Java Developer Interview Questions

    What is Multithreading in Java?

    Multithreading is a programmable approach to achieve multitasking. Multithreading in Java is a process of executing multiple threads cumulatively. A thread is the smallest unit of processing which is ...

    HCL Java Developer Interview Questions

    What use of intern() method in String?

    In Java, intern() is a native method of the String class. The String.intern () returns a reference to the equal string literal present in the string pool. The intern () method is applicable to String ...

    HCL Java Developer Interview Questions

    What is Collection Framework?

    A Java collection framework is an architecture that was added to Java 1.2 version. The Collection Framework provides a well-designed set of classes and interfaces for storing and manipulating a group ...