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 aberivation of java?

14 Answers  


Why should we use java?

1 Answers   IBM,


What do you understand by access specifiers in Java?

0 Answers   Genpact,


What are three advantages of using functions?

0 Answers  


Why is singleton instance static?

0 Answers  


What is an i/o filter?

5 Answers  


how to call one program .class file in another program

7 Answers   TCS,


What are facelets templates?

0 Answers  


What is break and continue statement?

0 Answers  


what are ER diagrams?

2 Answers  


what is object deep copy and shallow copy and why it is required?

2 Answers  


What is Exception handling in Java How do you handle run time errors please explain with an example

2 Answers   Mastek, TCS,


Categories