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 |
Can a class be protected in java?
What is the synchronized method modifier?
what is the swingutilities.invokelater(runnable) method for? : Java thread
What is the purpose of tostring() method in java?
What are the advantages of defining packages in java?
What is difference between static class and singleton pattern?
What are different types of constants?
What is the use of set in java?
What are some characteristics of interference class?
Can we make constructors static?
What is a Hash Table? What are the advantages of using a hash table?
Explain about assignment statement?