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

Can we declare variables inside a method as Final Variables?

3 Answers  


Differentiate between overriding and overloading cases?

0 Answers   Amdocs,


What is map and hashmap in java?

0 Answers  


Explain importance of inheritance in java?

0 Answers  


Explain the transient field modifier?

0 Answers  






is set interface synchronized by default???

2 Answers   Manhattan,


Write a java program to check if a number is prime or not?

0 Answers  


how a programmer confirms that the data submitted has been succesfully inserted into the database(either oracle or my sql).. How a programmer confirm if there is any problem with the program he wrote for insertion

2 Answers   SAP Labs,


Explain implementation and how is it different from conversion?

0 Answers   Aricent,


What are the advantages and disadvantages of object cloning?

0 Answers  


What is the Set interface?

2 Answers  


When we serialize an object does the serialization mechanism saves its references too?

0 Answers  


Categories