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
Can an interface extend a class?
Do I need java on my pc?
what is the messsage u r going to get from an objectoriented programing?
What is the difference between I ++ and ++ I in java?
What is the purpose of the return statement?
Can an anonymous class be declared as implementing an interface and extending a class in java programming?
What are dot operator queries?
In java, what is the difference between method overloading and method overriding?
State some advantages of java?
Why do we use return statement?
How we can run a jar file through command prompt in java?
Is void a type?
Explain differences between checked and unchecked exceptions in java?
Is an empty arraylist null?
What are the differences between checked exception and unchecked exception?