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

How to define a constant variable in Java?

1 Answers   TCS,


Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?

0 Answers  


What is the difference between interface & abstract class?

0 Answers  


When can we say that threads are not lightweight process in java?

0 Answers  


What is a byte array?

0 Answers  


Is null false in java?

0 Answers  


What is the java idl system?

0 Answers  


Why transient variables wont participate in serialization?

2 Answers  


In case of inheritance what is the execution order of constructor and destructor?

0 Answers   Agilent,


Hi am an mca graduate . i have done bsc maths in my degree . every company asks me why you make shift from maths group to computere field . What i need to answer?

2 Answers  


wHAT IS DEFAULT SPECIFIER IN JAVA wHAT IS DEFAULT CONSTRUCTOR IN JAVA wHAT IS DEFAULT METHOD IN JAVA

12 Answers   IBM,


what do you mean by java annotations?

0 Answers  


Categories