In Java, an array is a collection of the same data type that dynamically creates objects and can have elements of primitive types. Java allows us to store objects in an array.
An array of objects is a collection of multiple objects stored in a single variable. It stores the references to the objects. It is useful when managing multiple instances of a class efficiently.

Creating an Array of Objects
Before creating an array of objects, we must create an instance of the class by using the new keyword. We can use any of the following statements to create an array of objects.
Syntax:
- ClassName obj[]=new ClassName[array_length]; //declare and instantiate an array of objects
Or
- ClassName[] objArray;
Or
- ClassName objeArray[];
Suppose, we have created a class named Employee. We want to keep records of 20 employees of a company having three departments. In this case, we will not create 20 separate variables. Instead of this, we will create an array of objects, as follows.
- Employee department1[20];
- Employee department2[20];
- Employee department3[20];
The above statements create an array of objects with 20 elements.
Example: Creating an Array of Objects
In the following program, we have created a class named Product and initialized an array of objects using the constructor. We have created a constructor of the class Product that contains the product ID and product name. In the main() function, we have created individual objects of the class Product. After that, we passed initial values to each of the objects using the constructor.
Example
- public class Main {
- public static void main(String args[]) {
- //create an array of product objects
- Product[] obj = new Product[5] ;
- //create & initialize actual product objects using constructor
- obj[0] = new Product(23907,”Dell Laptop”);
- obj[1] = new Product(91240,”HP 630″);
- obj[2] = new Product(29823,”LG OLED TV”);
- obj[3] = new Product(11908,”MI Note Pro Max 9″);
- obj[4] = new Product(43590,”Kingston USB”);
- //display the product object data
- System. out.println(“Product Object 1:”);
- obj[0].display();
- System.out.println(“Product Object 2:”);
- obj[1].display();
- System.out.println(“Product Object 3:”);
- obj[2].display();
- System.out.println(“Product Object 4:”);
- obj[3].display();
- System.out.println(“Product Object 5:”);
- obj[4].display();
- }
- }
- //Product class with product ID and product name as attributes
- class Product {
- int pro_Id;
- String pro_name;
- //Product class constructor
- Product(int pid, String n) {
- pro_Id = pid;
- pro_name = n;
- }
- public void display() {
- System.out.print(“Product ID = “+pro_ID + ” ” + ” Product Name = “+pro_name);
- System.out.println();
- }
- }
Compile and Run
Output:
Product Object 1:
Product ID = 23907 Product Name = Dell Laptop
Product Object 2:
Product ID = 91240 Product Name = HP 630
Product Object 3:
Product ID = 29823 Product Name = LG OLED TV
Product Object 4:
Product ID = 11908 Product Name = MI Note Pro Max 9
Product Object 5:
Product ID = 43590 Product Name = Kingston USB
Initializing an Array of Objects
After the objects are instantiated, the array has to be initialized with values. In contrast to a variety of primitive types, an array of objects cannot be initialized in the same way. It is necessary to initialize each element or object in an array of objects. An array contains pointers to the real class through its objects.
Consequently, it is highly recommended to generate actual objects of the class after declaring and instantiating an array of objects. The constructors can set up the array. Actual objects can have their initial values assigned by the application by supplying values to the constructor. A class’s independent member method can also be used to assign data to an object.
The elements of an array of objects must be initialized after they have been created. Two common approaches are available to perform this:
- Using the Constructor
- Using Setter Methods
1. Using the Constructor
By providing variables to the constructor individually, we can define starting values for each object as the actual objects are being created. Every single real object is made with its unique values.
Example
- // Java program to demonstrate initializing an array of objects using a constructor
- public class Main {
- public static void main(String args[]) {
- // Declaring an array of Book
- Book[] books;
- // Allocating memory for 3 Book objects
- books = new Book[3];
- // Initializing the elements of the array using the constructor
- books[0] = new Book(101, “Java Basics”);
- books[1] = new Book(102, “Data Structures”);
- books[2] = new Book(103, “Operating Systems”);
- System.out.println(“Book details:”);
- for (int i = 0; i < books.length; i++) {
- books[i].display();
- }
- }
- }
- // Creating a Book class with id and title as attributes
- class Book {
- public int bookId;
- public String title;
- // Book class constructor
- Book(int bookId, String title) {
- this.bookId = bookId;
- this.title = title;
- }
- // display() method to display the book data
- public void display() {
- System.out.println(“Book ID: ” + bookId + “, Title: ” + title);
- }
- }
Compile and Run
Output:
Book details:
Book ID: 101, Title: Java Basics
Book ID: 102, Title: Data Structures
Book ID: 103, Title: Operating Systems
3. Using Setter Methods
Using setter methods, we may assign values after creating objects. The initial values of the objects are assigned using a member function of the corresponding class that is formed.
Example
- // Java program to demonstrate initializing an array of objects using setter methods
- public class Main {
- public static void main(String args[]) {
- // Declaring an array of Book
- Book[] books;
- // Allocating memory for 3 objects of type Book
- books = new Book[3];
- // Creating actual Book objects
- books[0] = new Book();
- books[1] = new Book();
- books[2] = new Book();
- // Assigning data to Book objects using the setter method
- books[0].setData(201, “Java Programming”);
- books[1].setData(202, “Algorithms”);
- books[2].setData(203, “Database Systems”);
- // Displaying the book data
- System.out.println(“Book details:”);
- for (int i = 0; i < books.length; i++) {
- books[i].display();
- }
- }
- }
- // Creating a Book class with id and title as attributes
- class Book {
- public int bookId;
- public String title;
- // Setter method to set the data
- public void setData(int bookId, String title) {
- this.bookId = bookId;
- this.title = title;
- }
- // display() method to show book details
- public void display() {
- System.out.println(“Book ID: ” + bookId + “, Title: ” + title);
- }
- }
Compile and Run
Output:
Book details:
Book ID: 201, Title: Java Programming
Book ID: 202, Title: Algorithms
Book ID: 203, Title: Database Systems
Leave a Reply