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 |
What is a website container?
What kind of variables can a class consist?
Why does java doesnt suuport unsigned values?
is java support call by reference and call by value, if supports please explain?
What are streams in java 8?
Why hashmap is used in java?
How many types of keywords are there?
What is a for loop in java?
Is there any limitation of using inheritance?
Write a program to create a binary Tree ?
What is the difference between abstraction and encapsulation?
How many bits is a double?