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 |
How core java/j2ee project performance can be measured ?
How do you check if a number is a perfect square?
How is hashcode calculated in java?
Why pointers are not used in java?
What do you mean by singleton class in java?
primitive data types in Java ?
Is constructor inherited?
what is difference between length and length()?
What design pattern you have used in your project? I answered Factory pattern, how it is implemented? What are its advantage? Do know about Abstract Factory?
Which of the classes will have more memory allocated?
Explain the selection sort algorithm?
Is array primitive data type in java?