Array of Objects
Arrays of objects in Java are collections that can store multiple instances of a
class. They follow a similar structure to arrays of primitive data types but
instead hold references to objects. Defining an array of objects involves
declaring the array variable and then allocating memory for the objects. Let’s
explore both of them.
Declaring Arrays of Objects
To declare an array of objects, the syntax involves specifying the class type
followed by square brackets []:
MyClass[] objectArray = new MyClass[5];
This line of code declares an array objectArray capable of holding 5 instances
of the MyClass objects. However, at this point, the array only contains null
references and doesn't hold any actual objects. It's essential to initialize each
element of the array to store objects.
Initialization and Memory Allocation of Array of Objects
Initializing an array of objects involves creating individual objects and
assigning them to each element of the array:
objectArray[0] = new MyClass();
objectArray[1] = new MyClass();
// ... (initializing other elements)
Here, each index in the objectArray is assigned a new instance of the MyClass
object using the new keyword. It's important to note that memory is allocated
for the objects themselves, not the array, and each element holds a reference to
an object.
Accessing Elements in Arrays of Objects
Accessing elements in arrays of objects is similar to accessing elements in
regular arrays. Each element can be accessed by its index.
MyClass obj = objectArray[0];
// Use obj to perform operations or access properties/methods of MyClass
Here, objectArray[0] retrieves the object reference stored at index 0, allowing
operations or access to the properties and methods of the MyClass object.
Iterating Through Array Elements
Iterating through an array of objects is commonly done using loops, like the
enhanced for loop or traditional for loop, to access each element:
for (MyClass obj : objectArray) {
// Perform operations on each obj (MyClass object)
}
Or using a traditional for loop:
for (int i = 0; i < objectArray.length; i++) {
MyClass obj = objectArray[i];
// Perform operations on obj
}
These iterations allow developers to access, manipulate, or perform operations
on each object stored within the array.
Example :
class Employee{
int empId;
String name;
//Employee class constructor
Employee(int eid, String n){
empId = eid;
name = n;
}
public void showData(){
System.out.print("EmpId = "+empId + " " + " Employee Name = "+name);
System.out.println();
}
}
class Main{
public static void main(String args[]){
//create array of employee object
Employee[] obj = new Employee[2] ;
//create & initialize actual employee objects using constructor
obj[0] = new Employee(100,"ABC");
obj[1] = new Employee(200,"XYZ");
//display the employee object data
System.out.println("Employee Object 1:");
obj[0].showData();
System.out.println("Employee Object 2:");
obj[1].showData();
}
}
Output :
c:\Java>javac Main.java
c:\Java>java Main
Employee Object 1:
EmpId = 100 Employee Name = ABC
Employee Object 2:
EmpId = 200 Employee Name = XYZ
Example 2 :
class Student {
String name;
int age;
// Constructor without using 'this'
Student(String n, int a) {
name = n;
age = a;
}
// Display method
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
// Create an array of Student objects
Student[] students = new Student[3];
// Initialize array elements
students[0] = new Student("Renuka", 22);
students[1] = new Student("Amit", 24);
students[2] = new Student("Sneha", 21);
// Using for loop
System.out.println("Using for loop:");
for (int i = 0; i < students.length; i++) {
students[i].display();
}
// Using for-each loop
System.out.println("\nUsing for-each loop:");
for (Student s : students) {
s.display();
}
}
}
Output:
Using for loop:
Name: Renuka, Age: 22
Name: Amit, Age: 24
Name: Sneha, Age: 21
Using for-each loop:
Name: Renuka, Age: 22
Name: Amit, Age: 24
Name: Sneha, Age: 21