Array of Objects in Java

(Last Updated On: February 27, 2019)

Learn how to create an array of objects in java with example code – An array of user defined class objects can also be created the same way as we create array of built-in type.

For example, here is an array for 5 int type.

int [] numbers = new int[5];

Similarly, an Array of 5 objects of the class Car can be created as below.

Car[] obj = new Car[5];

In an array of objects, all the objects will be of same class. [ Recommend to read how to create an object of a class in java]


We have created array for 5 Car objects in which we can store 5 objects of Car class.

Now for each 0 to 4 slot, we can create Car object as below.

obj[0] = new Car();

obj[1] = new Car();

obj[2] = new Car();

obj[3] = new Car();

obj[4] = new Car();


Array of objects java example

This program creates an array of 5 objects of class Car. Set the name of each car. Using for loop, it will retrieve each array elements and display the names of the cars on console.



Car : Maruti
Car : Honda
Car : Ford
Car : Hundai
Car : Nissan