Java

How to Create an Array of Objects in Java

When you need to store a single object in your program, you can use an Object variable. However, while dealing with a large number of objects, it is preferable to use an Array of Objects. It is important to note that when we say Array of Objects, we are referring to the object’s reference rather than the actual objects. The “[]” array notation in Java can be used to create an array of an object.

This guide will describe the procedures for creating Java array objects.

How to Create an Array of Objects in Java?

For creating an array of objects in Java, you can use the following approaches:

Let’s come to understand these methods with detailed examples.

Method 1: Create an Array of Objects in Java Using Declaration Process

In Java, the array of objects is created the same as the array creation process. In this section, we will create an array of objects using the declaration process.

The syntax for declaring an array of objects is given below.

Syntax

Follow the below-given syntax to create an array of objects:

ClassName [] objectName;

In Java, the class is also a user-defined data type.

You can also follow the below syntax:

ClassName objectName[];

Example: Declaring an Array of Object of One Class in Another Class

In this example, first we will create a “User” class that contains two variables, “id” and “Name”, a parameterized constructor and a method named “display()” that displays the variable values on the console:

classUser{
    int id;
    String Name;
    User(String name, int id){
        this.Name=name;
        this.id=id;
    }
    publicvoiddisplay() {
        System.out.print("Name is " + Name + " "+ "and the id is "+ id);
        System.out.println();      
    }
}

Now, in the main() method of another class named “Example”, first we will create an array of objects of “User” type by declaring an array that stores the objects. Then, we will allocate memory for objects with length “5”. After that, we will initialize the values for each index of the array and display the value of index “2” by calling the “display()” method of the “User” class:

publicclassExample {
    publicstaticvoidmain(String[] args) {
         User[] user;
         user= new User[5];
         user[0]=new User("John",1);
         user[1]=new User("Karley",2);
         user[2]=new User("Rohnda",3);
         user[3]=new User("Byrne",4);
         user[4]=new User("Kotley",5);
         System.out.print("User data in array's index 2: ");
         user[2].display();
    }
}

The output shows the value of the object at the 2nd index:

Let’s see another method for creating an array of objects.

Method 2: Create an Array of Objects in Java Using Declaration and Instantiation Process

In this section, we will create an array by declaring and instantiating it simultaneously. You can instantiate an array by using the “new” keyword and pass the length of the array in it. This approach is more efficient as it reduces the lines of code by handling the declaration and instantiation process at a time.

Syntax

The syntax for declaring and instantiating an array of objects is given below:

ClassName [] objectName = new ClassName[lengthofArray];

Example: Declaring and Instantiating an Array of Object of One Class in Another Class

In this example, we will create an array of objects named “user” by declaring and instantiating it simultaneously:

User[] user = new User[5];

Initialize the values to the objects as in the previous example and display the value of the object at index “3”, invoking “display()” method of the User class:

user[3].display();

The output shows the value of the user object at the 3rd index:

Now, let’s see the last method for creating an array of objects.

Method 3: Create an Array of Objects in Java Using Declaration and Initialization Process

For creating an array of objects, you can also initialize at the time of declaration using “{ }” curly braces:

Syntax

The following syntax is used for the creation of an array of objects:

ClassName [] objectName = {value1, value2,.....};

Here, we declare an array of class type and immediately initialize it with values.

Example: Declaring and Initializing an Array of Object of Predefined Object Class

In this example, we will create an array of objects of the “Object” type named “obj” and initialize it with values using “{ }” curly braces. Here, Object is the predefined Java class that is used here as a type of array:

Object[] obj= { "John", "Karley", "Rohnda", "Byrne", "Kotley"};

Then, we will print the value of the object at the 0th index using the “System.out.println()” method:

System.out.println(obj[0]);

The output indicates that the “John” is stored at the 0th index of the array of objects “obj”:

We have gathered all the ways for creating an array of objects in Java.

Conclusion

For creating an array of objects in Java, you can use different approaches like declaration, a declaration with instantiation, and a declaration with initialization. In Java, the array of objects is created the same as the array creation process because an object’s array stores the objects. Java permits you to create an array of objects of both user-defined and predefined classes. In this guide, we described the ways for the creation of an array of objects with examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.