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
what is bmg file and how to create that files?what will it contailn?
What is arraylist e in java?
How do you achieve polymorphism in java?
Can we serialize static variables in java?
Why put method is used?
What is java util?
What happens when I use / and % with a negative numerator?
What is the use of put method?
What is use of static in java?
Why are constructors used?
Which access specifier can be used with class ?
What is java in simple terms?
What is compareto?
Explain the importance of thread scheduler in java?
How do you use spaces in java?