Javatpoint Logo

could we create the object without using new keyword..if yes so how and explain all pls

By: yogesh*** On: Mon May 27 16:03:54 EDT 2013     Question Reputation15 Answer Reputation0 Quiz Belt Series Points0  15Blank User
could we create the object without using new keyword..if yes so how and explain all plsUp0Down

 
yes i think

with newinstanse();
Image Created0Down

By: [email protected] On: Tue May 28 02:40:00 EDT 2013 Question Reputation5 Answer Reputation0 Belt Series Points0 5User Image
Are You Satisfied :3Yes1No
 
There are four different ways to create objects in java:

A. Using new keyword This is the most common way to create an object in java. Almost 99% of objects are created in this way.

MyObject object = new MyObject();

B. Using Class.forName() If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
C. Using clone() The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();
D. 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();
You can read from here
Image Created0Down

By: [email protected] On: Tue May 28 03:20:31 EDT 2013 Question Reputation3 Answer Reputation57 Belt Series Points0 60User Image
Are You Satisfied :1Yes2No
 
You can create an object without new keyword and also you can access all methods with that object by writing code like below

ClassName obj = (ClassName )Activator.CreateInstance(typeof(ClassName ));
Image Created0Down

By: [email protected] On: Tue May 28 05:42:01 EDT 2013 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :0Yes1No