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 to define a constant variable in Java?
Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?
What is the difference between interface & abstract class?
When can we say that threads are not lightweight process in java?
What is a byte array?
Is null false in java?
What is the java idl system?
Why transient variables wont participate in serialization?
In case of inheritance what is the execution order of constructor and destructor?
Hi am an mca graduate . i have done bsc maths in my degree . every company asks me why you make shift from maths group to computere field . What i need to answer?
wHAT IS DEFAULT SPECIFIER IN JAVA wHAT IS DEFAULT CONSTRUCTOR IN JAVA wHAT IS DEFAULT METHOD IN JAVA
what do you mean by java annotations?