Different ways to create an/a object in java 2013

View previous topic View next topic Go down

Different ways to create an/a object in java 2013

Post by Admin on Sun Sep 15, 2013 4:04 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">In java, an object of a class can be created in various ways. This is the most important as well as most common question asked in java interviews. There are 4 difference ways to create a/an object in java as described below:<br /><br />1) <b>Using new keyword</b><br />2) <b>Using Class.forName()</b><br />3) <b>Using clone()</b><br />4) <b>Using object deserialization</b><br />Detailed explanation on different ways to create a object in java:<br /><br />Consider a class as shown below:<br /><br /><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> public class WaysToCreateObject implements Cloneable { <br /> public static void main(String args[]) throws InstantiationException, IllegalAccessException, <br /> ClassNotFoundException, CloneNotSupportedException { <br /> // Object created using the new keyword <br /> WaysToCreateObject objectNew = new WaysToCreateObject(); <br /> // Object created using Class.forName <br /> WaysToCreateObject objectClassForName = (WaysToCreateObject) Class.forName( <br /> "com.objectcreation.WaysToCreateObject").newInstance(); <br /> // Object created using clone <br /> WaysToCreateObject objectClone = (WaysToCreateObject) objectNew.clone(); <br /> } <br /> } </code><br /></pre>To read the complete post click below<br /><a name='more'></a><br /><br />As shown above, 1) <b>Using new keyword</b> : This is the most common and most used way to create a java object. Maximum java programmers use the new keyword to create a new object. Most simplest way. This way is also called as static instance creation of an object.<br /><br />2) <b>Using Class.forName()</b> : This way can be used, if the programmer knows the class name along with the package of the class. If you use this way, then the class must handle <b>throws InstantiationException, IllegalAccessException, ClassNotFoundException</b>.<br />Most important thing that the programmer needs to handle here is to check if the object created using this way do not have private constructor.<br /><br />3) <b>Using clone()</b> : This way ob object creation is done using clone() method of an object. Using this, the method gets created which is a duplicate copy of an original copy. All the properties gets copied in the new object.<br /><b> Most important thing when we create an object using this way is that the class must implement Cloneable interface</b>, otherwise it will throw CloneNotSupportedException. Also the class must throw CloneNotSupportedException, in order to proper compilation of a java class.<br /><br />4) <b>Using object deserialization</b> : This is done using the serialization/de-serialization in java. The object that is already serialized and is being stored somewhere, we need to de-serialize that object and get the object. This is being done as shown below <br /><br />ObjectInputStream inStream = new ObjectInputStream(anInputStream ); WaysToCreateObject object = (WaysToCreateObject) inStream.readObject(); </div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">In java, an object of a class can be created in various ways. This is the most important as well as most common question asked in java interviews. There are 4 difference ways to create a/an object in java as described below:<br /><br />1) <b>Using new keyword</b><br />2) <b>Using Class.forName()</b><br />3) <b>Using clone()</b><br />4) <b>Using object deserialization</b><br />Detailed explanation on different ways to create a object in java:<br /><br />Consider a class as shown below:<br /><br /><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> public class WaysToCreateObject implements Cloneable { <br /> public static void main(String args[]) throws InstantiationException, IllegalAccessException, <br /> ClassNotFoundException, CloneNotSupportedException { <br /> // Object created using the new keyword <br /> WaysToCreateObject objectNew = new WaysToCreateObject(); <br /> // Object created using Class.forName <br /> WaysToCreateObject objectClassForName = (WaysToCreateObject) Class.forName( <br /> "com.objectcreation.WaysToCreateObject").newInstance(); <br /> // Object created using clone <br /> WaysToCreateObject objectClone = (WaysToCreateObject) objectNew.clone(); <br /> } <br /> } </code><br /></pre>To read the complete post click below<br /><a name='more'></a><br /><br />As shown above, 1) <b>Using new keyword</b> : This is the most common and most used way to create a java object. Maximum java programmers use the new keyword to create a new object. Most simplest way. This way is also called as static instance creation of an object.<br /><br />2) <b>Using Class.forName()</b> : This way can be used, if the programmer knows the class name along with the package of the class. If you use this way, then the class must handle <b>throws InstantiationException, IllegalAccessException, ClassNotFoundException</b>.<br />Most important thing that the programmer needs to handle here is to check if the object created using this way do not have private constructor.<br /><br />3) <b>Using clone()</b> : This way ob object creation is done using clone() method of an object. Using this, the method gets created which is a duplicate copy of an original copy. All the properties gets copied in the new object.<br /><b> Most important thing when we create an object using this way is that the class must implement Cloneable interface</b>, otherwise it will throw CloneNotSupportedException. Also the class must throw CloneNotSupportedException, in order to proper compilation of a java class.<br /><br />4) <b>Using object deserialization</b> : This is done using the serialization/de-serialization in java. The object that is already serialized and is being stored somewhere, we need to de-serialize that object and get the object. This is being done as shown below <br /><br />ObjectInputStream inStream = new ObjectInputStream(anInputStream ); WaysToCreateObject object = (WaysToCreateObject) inStream.readObject(); </div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum