Javatpoint Logo

HOW MANY WAYS CREATE OBJECT FOR CLASS IN JAVA LANGUAGE?

By: sidduv*** On: Thu Mar 14 23:04:10 IST 2013     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
GIVE ME THE BRIEF ANSWERUp0Down

 
one ways ...

class Classname
{

}
Image Created0Down

By: [email protected] On: Fri Mar 15 04:15:37 IST 2013 Question Reputation0 Answer Reputation11 Belt Series Points0 11User Image
Are You Satisfied :6Yes4No
 
There are four different ways to create objects in java:

1. 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();

2. 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();

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();
Image Created0Down

By: [email protected] On: Mon Mar 18 17:34:44 IST 2013 Question Reputation0 Answer Reputation2 Belt Series Points0 2User Image
Are You Satisfied :17Yes4No
 
Image Created0Down

By: [email protected] On: Wed Aug 10 19:24:54 IST 2016 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
There are 5 approaches which can be used to create an object in Java. So let's start.

1) new keyword :

This approach we commonly use in Java to create an object.

Example :

ArrayList<String> list = new ArrayList<String>();

2) Class.forName() : / Reflection

This approach is also known as way of creating an object using reflection API. This simply returns the Class object associated with the class given. Commonly used in JDBC connection.

Example:

SampleClass sampleObj = Class.forName("SampleClass")

3) clone() :

This approach is used to create a copy of an object in Java.

Example:

ArrayList<String> list = new ArrayList<String>();
..
..
ArrayList<String> newList= null;
newList= list.clone();

4) deserialization() :

This is an approach where we get object from its serialized form and convert/deserialize

Example:

ObjectInputStream ois = new ObjectInputStream("<some inputstream obj>");
Object obj = ois.readObject();

5) newInstance() : / with ClassLoader way

This approach is some what related to the 2nd one. Here,

Example:

MyAppClass appObj = (MyAppClass)sampleObj.newInstance();

Image Created0Down

By: [email protected] On: Thu Mar 09 19:26:46 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
There are 5 approaches which can be used to create an object in Java. So let's start.

1) new keyword :

This approach we commonly use in Java to create an object.

Example :

ArrayList<String> list = new ArrayList<String>();

2) Class.forName() : / Reflection

This approach is also known as way of creating an object using reflection API. This simply returns the Class object associated with the class given. Commonly used in JDBC connection.

Example:

SampleClass sampleObj = Class.forName("SampleClass")

3) clone() :

This approach is used to create a copy of an object in Java.

Example:

ArrayList<String> list = new ArrayList<String>();
..
..
ArrayList<String> newList= null;
newList= list.clone();

4) deserialization() :

This is an approach where we get object from its serialized form and convert/deserialize

Example:

ObjectInputStream ois = new ObjectInputStream("<some inputstream obj>");
Object obj = ois.readObject();

5) newInstance() : / with ClassLoader way

This approach is some what related to the 2nd one. Here,

Example:

MyAppClass appObj = (MyAppClass)sampleObj.newInstance();

Image Created0Down

By: [email protected] On: Thu Mar 09 19:27:15 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No