Ways to create a Java Object

Object is an important concept in Java Development. This tutorial shows you the ways to create a Java Object.

I. Ways to create a Java Object

1. Using ‘new’ keyword

This is the most popular way to create an object.

MyJavaClass object = new MyJavaClass();

2. Using Class.forName()

Instead of calling new MyJavaClass(), we can call newInstance() method in this way:

MyJavaClass object = (MyJavaClass) Class.forName("com.javasampleapproach.createobject.MyJavaClass").newInstance();

3. Using newInstance() method of Constructor class

java.lang.reflect.Constructor class has newInstance() method which can be used to create objects.

Constructor constructor = MyJavaClass.class.getConstructor();
MyJavaClass object = constructor.newInstance();

4. Using clone() method

There will be no constructor to be invoked when using clone() method, the JVM generates exactly the object which content is the same as content of the object which call the method.

// we should implement Cloneable and define the clone() method before
MyJavaClass object2 = (MyJavaClass) object1.clone();

5. Using Object Deserialization

This way creates an object from its serialized form. We should implement a Serializable interface in the class before.

ObjectInputStream in = new ObjectInputStream(new FileInputStream("mydata.o"));
MyJavaClass object = (MyJavaClass) in.readObject();

II. Source Code


package com.javasampleapproach.createobject;

import java.io.Serializable;

public class MyJavaClass implements Cloneable, Serializable {

	private static final long serialVersionUID = 1L;

	public MyJavaClass() {
		System.out.println("Constructor for MyJavaClass ...");

	public Object clone() {
		try {
			return super.clone();
		} catch (Exception e) {
			return null;


package com.javasampleapproach.createobject;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

public class MainApp {

	public static void main(String[] args) throws Exception {

		// Using 'new' keyword
		MyJavaClass object1 = new MyJavaClass();

		// Using Class.forName()
		MyJavaClass object2 = (MyJavaClass) Class.forName("com.javasampleapproach.createobject.MyJavaClass")

		// Using newInstance() method of Constructor class
		Constructor constructor = MyJavaClass.class.getConstructor();
		MyJavaClass object3 = constructor.newInstance();

		// Using clone() method
		MyJavaClass object4 = (MyJavaClass) object3.clone();

		// Using Object Deserialization
		// Serialization
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("mydata.o"));
		// Deserialization
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("mydata.o"));
		MyJavaClass object5 = (MyJavaClass) in.readObject();

Run the code above, the console shows:

Constructor for MyJavaClass ...
Constructor for MyJavaClass ...
Constructor for MyJavaClass ...

By grokonez | February 5, 2017.

Last updated on April 27, 2021.

Related Posts

Got Something To Say:

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