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

MyJavaClass.java


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 ...");
	}

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

MainApp.java


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();
		System.out.println(object1);

		// Using Class.forName()
		MyJavaClass object2 = (MyJavaClass) Class.forName("com.javasampleapproach.createobject.MyJavaClass")
				.newInstance();
		System.out.println(object2);

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

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

		// Using Object Deserialization
		// Serialization
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("mydata.o"));
		out.writeObject(object4);
		out.close();
		// Deserialization
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("mydata.o"));
		MyJavaClass object5 = (MyJavaClass) in.readObject();
		in.close();
		
		System.out.println(object5);
	}
}

Run the code above, the console shows:


Constructor for MyJavaClass ...
com.javasampleapproach.createobject.MyJavaClass@2a139a55
Constructor for MyJavaClass ...
com.javasampleapproach.createobject.MyJavaClass@15db9742
Constructor for MyJavaClass ...
com.javasampleapproach.createobject.MyJavaClass@6d06d69c
com.javasampleapproach.createobject.MyJavaClass@7852e922
com.javasampleapproach.createobject.MyJavaClass@75b84c92


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 *

*