Saturday, January 12, 2013

Ways to create an object in Java


package com.objectcreation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ObjectCreation implements Cloneable,Serializable{

private String name="testname";

private int age=20;

private static long testValue;

private ObjectCreation(long testValue){

this.testValue = testValue;

}

public static ObjectCreation getInstance(){

return new ObjectCreation(007);
}


ObjectCreation(String name,int age){
this.name = name;
this.age = age;
}


ObjectCreation(ObjectCreation ocreation) {//CONSTUCTOR FOR COPY CONSTRUCTOR
this(ocreation.name,ocreation.age);
}



public static void main(String args[]) {
/*Method 1: using new operator */
ObjectCreation oc = new ObjectCreation("akkhil",10);
System.out.println("Object created using new operated is"+oc.toString());

/*Method 2: copy constructor*/
ObjectCreation oc1 = new ObjectCreation(oc);
System.out.println("Object created using copy constructor is--------------------->"+oc1.toString());

/*method 3:Static factory method**/

ObjectCreation.getInstance();
System.out.println("Object created using static factory method............"+ObjectCreation.getInstance().testValue);

/*Method 4:Using Reflection**/
/* try {
Class cs = Class.forName("com.objectcreation.ObjectCreation");
try {
ObjectCreation ocnew = (ObjectCreation)cs.newInstance();
System.out.println("OBJECT CREATED USING REFLECTION..............."+ocnew.hashCode());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

*/
/**Method 5: Using clone*/
try {
ObjectCreation clonedObject = (ObjectCreation) oc.clone();
System.out.println("Object created using clone is-----------------"+clonedObject.hashCode());
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/**Method 6: Using Deserialization
*
****/
File f = new File("C:\\Users\\Dell\\Desktop\\SerializedFile.ser");
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(oc);
oos.close();
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}


try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
try {
Object ob = ois.readObject();
System.out.println("Object created from deserializatuon is>>>>>>>>>>>>>>>>>>>>>>>"+ob.hashCode());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

/**
* Method 7: Using Constructor
*/
Class clazz = String.class;
Constructor cons = null;
try {
cons = clazz.getConstructor(new Class[]{clazz});
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
System.out.println("Object created using constructor is>>>>>>>>>>>>"+cons.newInstance(new Object[]{101}));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

No comments:

Post a Comment