How to Create Array of Objects in Java?

In Java, arrays are used to store multiple values of the same data type in a single variable. While arrays can hold primitive data types like int, char or double they can also store objects of any class type. This allows to create arrays of objects where each element in the array is an instance of a specific class. In this article, we'll explore various ways to create and manipulate arrays of objects in Java.


Understanding Arrays of Objects:

An array of objects in Java is essentially an array where each element is a reference to an object of a particular class. This means that the elements of the array do not actually store the objects themselves but rather references to where the objects are stored in the heap memory.

Syntax for Creating an Array of Objects:

The syntax for creating an array of objects in Java is as follows:

ClassName[] arrayName = new ClassName[arraySize];

Where:
  • ClassName is the name of the class whose instances will be stored in the array.
  • arrayName is the name of the array variable.
  • arraySize is the number of elements in the array.

Example :

// Define a class named A
class A {
    String name;
    int id;
    // Constructor
    public A(String name, int id) {
        this.name = name;
        this.id = id;
    }
}
// Create an array of A objects
A[] b = new A[5];

In this example, we define a class named A with two instance variables: name and id. We then create an array named b that can hold up to 5 A objects.

Initializing Array Elements:

Once you have created an array of objects, you can initialize its elements by creating instances of the class and assigning them to the array elements.

// Initialize array elements
A[0] = new B("Kumar", 101);
A[1] = new B("M", 102);
A[2] = new B("Bob", 103);
A[3] = new B("A", 104);
A[4] = new B("B", 105);

In this example, we create instances of the B class and assign them to the array elements A[0] through A[4].

Accessing Array Elements:

You can access individual elements of the array using array indexing. Once you have access to an array element you can manipulate its properties or invoke its methods just like any other object.

// Access array elements
System.out.println(A[0].name);
System.out.println(A[1].id); 


In this example, we access the name property of the A object stored in A[0] and id property of the A object stored in A[1].

Iterating Over Array Elements:

You can iterate over the elements of an array of objects using a loop such as a for loop or a foreach loop.

// Iterate over array elements
for (int i = 0; i < A.length; i++) {
    System.out.println(A[i].name + " - " + A[i].id);
}

In this example, we use a for loop to iterate over each element of the A array and print the name and id of each A object.

Conclusion:

In Java, arrays of objects provide a convenient way to store and manipulate collections of objects. By creating an array of a specific class type you can store multiple instances of that class in a single variable. This allows you to efficiently manage and access related objects within your Java programs. Understanding how to create, initialize, access and iterate over arrays of objects is an essential skill for Java developers.

Post a Comment

Previous Post Next Post