Creating an Array of Objects in Java
We can create an array of objects in java using the 'Object' class.
Using the following statement, you create an array of objects.
NameOfClass ArrayOfObjects[];
Alternatively, you can declare an array of objects using the following statement.
NameOfClass[] ArrayOfObjects;
Note In both statements, "ArrayOfObjects" is an array of objects.
Example
If you have a class 'Ninjas', you can create an array of 'Ninjas' objects using the following statement.
Ninja NinjaObjects[];
Or
Ninja[] NinjaObjects;
You can also declare an array of objects with initial values. You can declare similarly to the given below statement.
NameOfClass ArrayOfObjects[] = {"something"};
Note Just like an array of primitive data types here also, you can give multiple values separated with a ','.
Example
Ninja NinjaObjects[] = {"NameOfNinja", new Integer(1)};
Application
Suppose we want to create a class named 'College'. We want to keep records of 40 students of a batch having four courses. So, in this case, we will create an array of objects instead of 40 separate variables.
College Course_1[40];
College Course_2[40];
College Course_3[40];
College Course_4[40];
Instantiate the array of objects
So far, we have declared the array of objects. Now we will instantiate it using the 'new' keyword before using it in the program.
To declare and instantiate the array of objects in java, use the following syntax.
NameOfClass ArrayOfObjects[] = new NameOfClass[ArraySize];
Note Using the above syntax, you can create an array of objects 'ArrayOfObjects' with an 'ArraySize' number of objects/elements references.
Example
The following statement will create an array named 'quad' with four elements. These four elements will store the objects of the class 'Quadrilateral'.
Quadrilateral quad[] = new Quadrilateral[4];
Initializing Array Of Objects
After you instantiate the array of objects, you need to initialise it with values. Let us learn two methods to initialise the array of objects.
-
Method 1: Using the constructor - You can initialise each object by passing values to the constructor separately.
-
Method 2: Using a member method of class - You can initialise each object using the class member method.
Now let us try to implement both methods using a program in java.
1. By using the constructor:
First, declare an array of objects of the desired type and then create an instance for each element in class, and finally passed the values to the constructor for each element.
The below program shows how the array of objects is initialized using the constructor.
Java
/*Array of Objects in Java*/
class Square {
private int length;
public Square(int l) {
length = l;
}
public int ComputeArea() {
return length * length;
}
}
class Test {
public static void main(String[] args) {
/*declaring array of objects*/
Square[] obj = new Square[1];
/*initialising the array*/
obj[0] = new Square(2);
for (int i = 0; i < 1; i++) {
System.out.println(obj[i].ComputeArea());
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
4
2. By using a separate member method :
First, declare an array of objects of the desired type, then create a separate member method in the class to initialize the objects in the array. Lastly pass the desired values as parameters and assign them.
The below program shows how the array of objects is initialized using a separate member method.
Java
/*Array of Objects in Java*/
class Rectangle {
private int length;
private int breadth;
public void assignData(int len, int bre) {
length = len;
breadth = bre;
}
public int ComputeArea() {
return length * breadth;
}
}
class Test {
public static void main(String[] args) {
/*declaring array of objects*/
Rectangle[] obj = new Rectangle[1];
/*Rectangle object*/
obj[0] = new Rectangle();
/*Assigning values to object*/
obj[0].assignData(4,5);
for (int i = 0; i < 1; i++) {
System.out.println(obj[i].ComputeArea());
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
20
Let’s see another example where an Array of Objects is declared with Initial Values:
Java
class Ninjas {
private String name;
private int exprience;
public Person(String name, int exprience) {
this.name = name;
this.exprience = exprience;
}
}
public class Main {
public static void main(String[] args) {
Ninjas[] people = {
new Ninjas("Dhruv", 20),
new Ninjas("Rawat", 21),
};
for (Ninjas coders : people) {
System.out.println(coders.name + " " + coders.exprience);
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Dhruv 20
Rawat 21
In the above code, we first create an array of two Ninjas objects, and then the first object is initialized with the name "Dhruv" and the experience 20. Similarly, the second object is initialized with the name "Rawat" and the experience 21. Hne we declared an Array of Objects with Initial Values
We hope you understand how to create an array of objects in java. Practice it on online java compiler.
Frequently Asked Questions
How does an array of objects work in Java?
An array of objects is a collection of references to objects in Java. The references are stored in the array, and each reference points to an object in memory. The objects in the array can be accessed by their index in the array.
How to create array of objects dynamically in Java?
In Java, you can create an array of objects dynamically using the 'new' keyword. First, define the class of objects. Then, declare an array and allocate memory for objects using 'new', and initialize the array elements with 'new' again
Why do we need an array of objects?
When we want to store a collection of objects, we need an array of objects. For example, an array of objects can be used to store a list of items, and employees details etc.
How do you store data in an array of objects?
To store data in an array of objects, we need to create a new object and assign it to a reference in the array. The syntax looks like this Ninjas coders = new Ninjas("Dhruv Rawat", 20).
How to find value in array of objects in Java?
In Java, finding a value in an array of objects involves creating the array, specifying the target value, and iterating through it. During each iteration, you compare the object's property with the target. If you find a match, you can take the necessary action. This approach is flexible and useful for various data types and search criteria.
Conclusion
This article discussed how to create an array of objects in Java. We learnt to declare, instantiate and two ways to initialise the array of objects.
We hope this blog on creating an array of objects in Java was helpful. You can refer to other similar articles as well -
You can also consider our paid courses such as DSA in Java to give your career an edge over others!
Happy Learning Ninja!