How can we create a object of a class without using new
operator.

Answer Posted / mamatha

There are different ways to create objects in java:

1. Using new keyword
This is the most common way to create an object in java.

MyObject object = new MyObject();

2. Using Class.forName()

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

3. Using clone()
The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

5.By using getInstance();

Is This Answer Correct ?    38 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the synonym of framework?

658


Where is the singleton class used?

680


What is passing parameters in java?

606


Why are variables important in research?

639


How do I know if java is installed?

629






Is arraylist sorted in java?

615


Why java is secure? Explain.

683


Does garbage collection occur in permanent generation space in jvm?

709


What is a void in java?

667


Explain scope or life time of local variables in java?

659


What is the main purpose of serialization in java?

637


What is purpose of keyword void?

652


What are class members by default?

747


Explain treeset?

679


What is bifunction in java?

767