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

What language is pass by reference?

760


How does list work in java?

682


Does java allow overriding static methods ?

824


Define nashorn in java8.

774


What is difference between filereader and bufferedreader?

709


How to add menushortcut to menu item?

734


What is boolean keyword in java?

857


How you can force the garbage collection?

727


What is the statements?

791


How do you write methodology?

728


Which java ide is used the most?

734


What invokes a thread's run() method in java programming?

783


what is the difference between future and callable interface in java?

840


Is singleton a bad practice?

794


How is string immutable in java?

757