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 declare variables inside a method as Final Variables?
Differentiate between overriding and overloading cases?
What is map and hashmap in java?
Explain importance of inheritance in java?
Explain the transient field modifier?
is set interface synchronized by default???
Write a java program to check if a number is prime or not?
how a programmer confirms that the data submitted has been succesfully inserted into the database(either oracle or my sql).. How a programmer confirm if there is any problem with the program he wrote for insertion
Explain implementation and how is it different from conversion?
What are the advantages and disadvantages of object cloning?
What is the Set interface?
When we serialize an object does the serialization mechanism saves its references too?