How Many Ways to Create Object in Java?

How many ways to create object in Java? The answer is there are many ways but in this tutorial I am sharing the 5 ways to do this.

  1. Using new Keyword
  2. Using object cloning
  3. Using Class.forName()
  4. Using Class Loader
  5. Using Object Deserialization

Below I have discussed each of these methods one by one with example.

 

How Many Ways to Create Object in Java?

How Many Ways to Create Object in Java?

Method 1: Using new Keyword

It is most common and popular method to create object in Java.

Example:

package com;

public class DemoClass {
	void printMessage() {
		System.out.println("Hello World");
	}
	
	public static void main(String args[]) {
		DemoClass object = new DemoClass();
		object.printMessage();
	}
}

Method 2: Using Object Cloning

We can create clone of an existing object using clone() method. The class must implement Cloneable interface. It can be done in following way.

Example:

package com;

public class DemoClass implements Cloneable{
	void printMessage() {
		System.out.println("Hello World");
	}
	
	public static void main(String args[]) {
		DemoClass object = new DemoClass();
		
		try {
			DemoClass copyObject = (DemoClass) object.clone();
			copyObject.printMessage();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}		
	}
}

Method 3: Using Class.forName()

Example: 

public class DemoClass {	
	void printMessage() {
		System.out.println("Hello World");
	}
	
	public static void main(String args[]) {
		try {
			DemoClass object = (DemoClass) Class.forName("com.DemoClass").newInstance();
			object.printMessage();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Method 4: Using Class Loader

Example: 

public class DemoClass {	
	void printMessage() {
		System.out.println("Hello World");
	}
	
	public static void main(String args[]) {
		try {
			DemoClass object = (DemoClass) DemoClass.class.getClassLoader().loadClass("com.DemoClass").newInstance();
			object.printMessage();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Method 5: Using Object Deserialization

Serialization means converting an object to byte stream to save it somewhere. Deserialization means obtaining object from its serialized form. This another way to create object. The class must implement Serializable interface.

Example: 

package com;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class DemoClass implements Serializable{
	String message;
	
	void printMessage() {
		System.out.println(message);
	}
	
	public static void main(String args[]) {
		try {
			DemoClass object = new DemoClass();
			object.message = "Hello World";
			
			//write an object to file
			FileOutputStream fos = new FileOutputStream("demo.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(object);
			
			//reading the object from file
			FileInputStream fis = new FileInputStream("demo.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			DemoClass newObject = (DemoClass) ois.readObject();
			
			newObject.printMessage();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Comment below if you have doubts or know other ways to create object in java.

Leave a Comment

Your email address will not be published. Required fields are marked *