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 we access the super class method using subclass object?
Is it possible to cast an int value into a byte variable? What would happen if the value of int is larger than byte?
What is the difference between applet and application?
How can i add a button in applet in java ???
Which api is provided by java for operations on set of objects?
Advantages of Inheritance in java.
When should I use stringbuffer?
Is treeset sorted in java?
How many digits is int32?
Why Do I Get A "permission Denied" Error After Downloading The .jnlp Java Launcher For The Vkvm?
What are the different types of methodologies?
what is generics in jdk1.5?