How to create array of objects in Java
Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates objects. Java allows us to store data of primitive type, or even objects.
An object is the variable of particular class type. When we want to create a single object, we create it with the help of Object class. However, to create multiple objects, it is preferred to use array of objects. Array of objects as the name suggests, holds the sequence of objects i.e., the objects are stored as array elements.
Note: the Arrays of Objects doesn’t store the object itself, rather it stores the reference of the object.
Creating an array of objects
In order to create an array of objects, we use the Java Object class (root class of all the objects).
Syntax:
Class_name [ ] object_reference ;
OR
Class_name object_reference [ ];
Example:
If we are creating a class Student and we have to store data (stud_id, stud_name, department_name ) of 30 students of an institute, we can use array of objects. Here, we cannot create 30 different objects, rather we will create the array of objects as:
Student[ ] stud_object;
OR
Student stud_object [ ];
Above code creates array 30 Student class objects.
Along with declaration array of objects need to be instantiated with new keyword.
Instantiating the array of objects:
The syntax to declare and instantiate array of objects is:
Class_name [ ] object_reference = new Class_name [array_length];
Example:
Student [ ] stud_object = new Student [5];
The above statement creates an array of objects named stud_object which can hold 5 object references.
Once the array of object is declared and instantiated, individual elements of array of objects needs to be created using the new keyword.
Initializing the array of objects:
The array of objects holds references to the actual class objects. Therefore, we need to create actual objects of a class after the declaration and instantiation of array of objects.
Unlike the basic initialization primitive type arrays, we initialize each element of our array of objects (i.e. each object reference) using the new keyword.
There are two ways to initialize the array of objects:
- Using constructors
- Using separate member method
Using constructors:
While creating the individual objects, we can initialize each object by passing values using the constructor.
Example 1:
Let’s consider the below example where we are creating the class Student and initializing the object references using the constructor of the class.
ArrayOfObjects.java
public class ArrayOfObjects{
public static void main(String[] args) {
//creating array of objects with length 3
Student[] stud_object = new Student[3];
//creating and initializing actual objects using constructor
stud_object[0] = new Student(121, "Riya", "Computer");
stud_object[1] = new Student(211, "Sham", "E&TC");
stud_object[2] = new Student(401, "Param", "Civil");
//printing the Student data
System.out.println("\nStudent object 1:");
stud_object[0].display();
System.out.println("\nStudent object 2:");
stud_object[1].display();
System.out.println("\nStudent object 3:");
stud_object[2].display();
}
}
//creating Student class
class Student {
public int stud_id;
public String stud_name;
public String dept_name;
//Student class constructor
Student (int id, String name, String dept){
this.stud_id = id;
this.stud_name = name;
this.dept_name = dept;
}
//method to display student data
public void display(){
System.out.println("Student id: " +stud_id );
System.out.println("Student name: " +stud_name );
System.out.println("Department of student: " +dept_name );
}
}
Output:

Using separate member method:
A member function of a class can be used to initialize the individual objects of an array.
Example 2:
Let’s consider an example where we create class Student and a method init_object() to assign values to the individual objects of the array.
public class ArrayOfObjects2{
public static void main(String[] args) {
//creating array of objects with length 2
Student[] stud_object = new Student[2];
//creating actual Student objects
stud_object[0] = new Student();
stud_object[1] = new Student();
//assigning values to student objects
stud_object[0].init_object(111, "Nirnay", "Computer");
stud_object[1].init_object(301, "Juli", "E&TC");
//printing the Student data
System.out.println("\nStudent object 1:");
stud_object[0].display();
System.out.println("\nStudent object 2:");
stud_object[1].display();
}
}
//creating Student class
class Student {
public int stud_id;
public String stud_name;
public String dept_name;
//methods to assign values to student objects
public void init_object(int id, String name, String dept){
this.stud_id = id;
this.stud_name = name;
this.dept_name = dept;
}
//method to display student data
public void display(){
System.out.println("Student id: " +stud_id );
System.out.println("Student name: " +stud_name );
System.out.println("Department of student: " +dept_name );
}
}
Output:

In this way, we have learned how to create array of objects in Java.