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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Can an interface be final?

550


What is defined as false sharing in the context of multithreading?

507


define the terminology association.

619


Can we make constructors static?

727


Difference between arraylist and hashset in java?

547






what is a thread pool in java and why is it used?

532


What is the different between get and post?

503


Can you give names of Container classes?

1857


What does n mean in java?

504


How many bytes is a string?

565


What is a java applet? What is an interface?

635


What is assembly condition codes?

558


How do I run java on windows?

531


How do you sort a string in alphabetical order in java?

537


what is the final keyword denotes in java?

586