Signup/Sign In

How to Create an Object in Java

Java is an Object-Oriented Programming Language. Java supports to Class and Object creation which a fundamental concept on OOP's.

In this tutorial, we will learn how to create an object of a class. Class is an entity it represents a real-life entity that has properties like real-life entities. Let's take a famous example of a car. A car can have a color, brand name, year of manufacture, and other different properties.

We represent that real-life entity using a class like this:

public class Car 
{
	 private String car_color;
	 private String brand;
	 private String fuel_type;
	 
	 public void Start()
	 {
		 ...
	 }	 
	 public void Stop()
	 {
		...
	 }	
	 
}

This given class is a blueprint for how we should make a car and like we produce a car from the blueprint we create an object of a class, it is also known as an instance of a class.

To create an object below is the infographic to understand object creation:

1:It is the name of a class

2: It is the name of an object which we want to create. It is a reference to an object.

3: It is the new keyword that allocates the memory for an object of a class.

4: It looks like a function but it is a Constructor that invokes automatically at the time of object creation.

enlightened The very interesting fact is an object used to create in a Heap Memory and Reference used to stored in Stack.

Example of object creation in Java:

In the below code we created an object car of Car class using new keyword for each object we can call methods using . (dot) operator. Here we called Start() and Stop() methods using car object.

public class Car 
{
	 private String car_color;
	 private String brand;
	 private String fuel_type;	 
	 public void Start()
	 {
		 System.out.println("Car Started");
	 }	 
	 public void Stop()
	 {
		 System.out.println("Car Stopped");
	 }		 
	 public static void main(String args[])
	 {
		 //creating an object of Car class
		 Car car = new Car();		
		 //Calling method Start() using object
		 car.Start();		 
		 //Calling method Stop() using object
		 car.Stop();
	 }	 
}


Car Started
Car Stopped

In most cases, we will do object creation using the above methods but there are more methods to create an object.

Example: Using newInstance() method of Class class

In the program given below, we created an object using a newInstance() method of a Class class. In this method instead of the new keyword, we use the method, and then this method will create an instance of a class and it will be stored to the reference.

class Student
{
	String stud_name;
	int roll_no;
}
public class StudyTonight  
{  
	public static void main(String args[]) throws Exception
	{  
		//creating an object using newInstance() method
		Student arr = Student.class.newInstance();
		arr.stud_name = "ABC";
		arr.roll_no = 123;		
		//printing data ob an object
		System.out.print("Name: "+arr.stud_name+" Roll No: "+arr.roll_no);		 
	}  
}  


Name: ABC Roll No: 123

Example: Using clone() method

In the program given below, we are creating an object of Student class using the clone() method. This method can be used if and only if at least one object of the class is already created. Also makes sure that Cloneable class is implemented in a class. While calling the clone() method we required to override it because it is protected.

class Student implements Cloneable
{
	String stud_name;
	int roll_no;
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}
public class StudyTonight  
{  
	public static void main(String args[]) throws Exception
	{  
		Student student = new Student();
		student.stud_name = "ABC";
		student.roll_no = 123;		
		//cloning student object to obj
		Student obj = (Student) student.clone();		
		//printing data of an object
		System.out.print("Name: "+obj.stud_name+" Roll No: "+obj.roll_no);		 
	}  
}  


Name: ABC Roll No: 123

Example: Creating an Object Using Deserialization

In the example given below, we are creating an object of the class using deserialization and this method is more about serialization and deserialization than creating an object. Firstly make sure that Class of which you want to create an object must be a serializabel to do so implement a Serializable class. After this simply you need to deserialize an object and the while deserialization assign that to new object.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable
{
	String stud_name;
	int roll_no;
}
public class StudyTonight  
{  
	public static void main(String args[]) throws Exception
	{  
		Student student = new Student();
		student.stud_name = "ABC";
		student.roll_no = 123;			
		// Serialization of a student object
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"))){
            out.writeObject(student);
        }
        
        Student obj;
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"))) {
            obj = (Student) in.readObject();
        }		
		//printing data of an object
		System.out.print("Name: "+obj.stud_name+" Roll No: "+obj.roll_no);		
		
	}  
}  


Name: ABC Roll No: 123

mailConclusion:

Class is a blueprint for an object that represents a real-life entity. The object is s instance of a class that creates in a Heap but reference to an object stored in a Stack.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.