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



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

Answer / 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

More Core Java Interview Questions

Explain treeset?

0 Answers  


how to split string in java?

0 Answers  


What is the formula to calculate percentage?

0 Answers  


What is a treeset in java?

0 Answers  


How do you write a good declaration?

0 Answers  






When do we use synchronized blocks and advantages of using synchronized blocks?

0 Answers  


Give some features of interface?

0 Answers  


What is thread pool in java with example?

0 Answers  


What is compareto () in java?

0 Answers  


I want to re-reach and use an object once it has been garbage collected. Define how it’s possible?

0 Answers  


Give me some null interfaces in java?

0 Answers  


What is parsing in grammar?

0 Answers  


Categories