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

What is the length of a string?

0 Answers  


How does multithreading take place on a computer with a single cpu?

0 Answers  


What is the maximum length of a url?

0 Answers  


Define interface?

3 Answers   MindCracker,


Difference between Primary key and unique key?

4 Answers  






What is object english?

0 Answers  


Default layout of panel and frame?

3 Answers  


Define inheritance?

0 Answers  


What is the difference between equals() and == in java?

0 Answers  


What is initial size of arraylist in java?

0 Answers  


What does t in java mean?

0 Answers  


What happens if we override private method?

0 Answers  


Categories