Java : Object Creation Way


In java objects can be created by five ways as follows:

  1. Object Creation by new
  2. Object Creation by reflection Class.newInstance() method.
  3. Object Creation by clone() method
  4. Object creation by the Annonymous Object.
  5. Object Creation by  Deserialization

Object Creation Way Example

Here is a complete example after considering all ways for object creations:

ackage objects;

import java.io.Serializable;

//Cloneable and Serializable is marker interface
public class Calculation implements Cloneable, Serializable{
	public int x=5;
	void fact(int n) {
		int fact = 1;
		for (int i = 1; i <= n; i++) {
			fact = fact * i;
		}
		System.out.println("factorial is " + fact);
	}
	public Object clone()throws CloneNotSupportedException{
		return super.clone();
		} 

}

package objects;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectCreationWay {

	public static void main(String[] args) {
		System.out.println("Object Creation by New");
		// Object Creation by using New
		Calculation cal = new Calculation();
		cal.fact(3);

		System.out.println("\nObject Creation by Reflection");
		// Object creation by newInstance method by reflection
		try {
			Class calculationClass = Class
					.forName("objects.Calculation");
			Calculation calc = (Calculation) calculationClass.newInstance();
			calc.fact(5);
		} catch (ClassNotFoundException |IllegalAccessException |InstantiationException ex) {
			ex.printStackTrace();
			//Multiple exception in single catch example in java  7
		} 

		System.out.println("\nObject Creation by Clone");
		/**
		 * Object creation by clone
		 */
		try {
			System.out.println(cal.x);
			Calculation cal2 = (Calculation) cal.clone();
			System.out.println(cal2.x);
			cal.x=10;
			//No change on clone object
			System.out.println(cal2.x);
		} catch (CloneNotSupportedException ex) {
			ex.printStackTrace();
		}

		System.out.println("\nObject Creation by Annonymous");
		/**
		 * Annonymous simply means nameless.An object that have no reference is
		 * known as annonymous object. If you have to use an object only once,
		 * annonymous object is a good approach.
		 */
		// calling method with annonymous object
		new Calculation().fact(5);

		System.out.println("\nObject Creation by  Deserialization");
		//By using deserialization of serialize object

		searializeObject(); //method to serialization of calculation object

		//By deserialization create object of Calculation Object

		Calculation c=deserializationOfObject();
		c.fact(5);

	}
	//serialize from java object to Object Stream
	public static void searializeObject()
	{
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(
					"calculator.ser");

			ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);

			Calculation c= new Calculation();
			c.x=12;
			//serialization of object
			objectOutputStream.writeObject(c);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	//deserialization of objectStream to Java object
	public static Calculation deserializationOfObject()
	{

			Calculation c = null;

			try {
				FileInputStream fileInputStream = new FileInputStream("calculator.ser");

				ObjectInputStream objectInputStream = new ObjectInputStream(
						fileInputStream);

				c = (Calculation) objectInputStream.readObject();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {

				e.printStackTrace();
			}

			return c;
	}

}

Output


Object Creation by New
factorial is 6

Object Creation by Reflection
factorial is 120

Object Creation by Clone
5
5
5

Object Creation by Annonymous
factorial is 120

Object Creation by  Deserialization
factorial is 120

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s