Arrays of Objects in Java
In Java, you can create arrays not only for primitive types (like int, double) but also for objects, including user-defined classes. Arrays of objects are widely used to store multiple instances of a class in a single container, making data management efficient.
Why Use Arrays of Objects?
- Organize related objects in a single container.
- Efficient for storing multiple user-defined types.
- Useful in real-world applications, such as:
- School management → array of
Studentobjects. - Employee management → array of
Employeeobjects. - Inventory systems → array of
Productobjects.
- School management → array of
Declaring and Creating Arrays of Objects

Step 1: Declare the Array
Student[] students; // Declares an array of Student objects
Step 2: Allocate Memory
students = new Student[3]; // Array can hold 3 Student references
At this point, students[0], students[1], students[2] are null because no objects have been created yet.
Step 3: Initialize Objects
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
void display() {
System.out.println(id + " " + name);
}
}
public class ArrayOfObjectsExample {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student(1, "Aman");
students[1] = new Student(2, "Neha");
students[2] = new Student(3, "Ravi");
for (Student s : students) {
s.display();
}
}
}
Output:
1 Aman
2 Neha
3 Ravi
Points to Remember
- Arrays store references, not the actual objects.
- Always instantiate each object before use to avoid
NullPointerException. - Use
array.lengthto get the number of object references. - Arrays of objects can be multidimensional:
Student[][] classes = new Student[2][3];
- For-each loop provides clean iteration syntax:
for (Student s : students) {
s.display();
}
Polymorphism with Arrays of Objects
You can store subclass objects in a superclass array:
class Animal { void sound() { System.out.println("Some sound"); } }
class Dog extends Animal { void sound() { System.out.println("Woof"); } }
class Cat extends Animal { void sound() { System.out.println("Meow"); } }
public class PolymorphismArrayExample {
public static void main(String[] args) {
Animal[] animals = new Animal[2];
animals[0] = new Dog();
animals[1] = new Cat();
for (Animal a : animals) {
a.sound(); // Runtime polymorphism
}
}
}
Output:
Woof
Meow
This demonstrates runtime polymorphism using arrays of objects.
Common Operations on Arrays of Objects
| Operation | Example |
|---|---|
| Access object fields | students[0].name |
| Invoke methods | students[1].display() |
| Iterate array | for(Student s : students) { s.display(); } |
| Sorting objects | Use Arrays.sort() with Comparable/Comparator |
| Copying object references | Use loops or System.arraycopy() |
Points to Remember:
- Arrays of objects store references, not objects themselves.
- Each object must be instantiated before usage.
- Supports polymorphism, methods, sorting, and searching like primitive arrays.
- Can be combined with Collections for dynamic sizing.
