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.
🏆 Top 5 Interview Questions – Arrays of Objects in Java
1. How do you declare and initialize an array of objects?
- Declaration:
Student[] students;
- Memory allocation:
students = new Student[3]; // Holds references, all are null initially
- Initialization:
students[0] = new Student(1, "Aman");
2. Can arrays store different object types?
Answer: Yes, using superclass references (polymorphism):
Animal[] animals = new Animal[2];
animals[0] = new Dog();
animals[1] = new Cat();
- Allows runtime polymorphism when calling overridden methods.
3. How do you iterate over an array of objects?
Answer:
- Using for loop:
for (int i = 0; i < students.length; i++) {
students[i].display();
}
- Using for-each loop:
for (Student s : students) {
s.display();
}
4. How do arrays of objects differ from primitive arrays?
Answer:
| Feature | Primitive Array | Array of Objects |
|---|---|---|
| Stores | Actual values | References to objects |
| Default | 0, false, '\0' | null |
| Operations | Direct value manipulation | Must instantiate objects first |
5. How can you sort or copy arrays of objects?
Answer: Sorting: Use Arrays.sort() with Comparable or Comparator:
Arrays.sort(students, (s1, s2) -> s1.id - s2.id);
- Copying references: Use loops or
System.arraycopy():
Student[] copy = new Student[students.length];
System.arraycopy(students, 0, copy, 0, students.length);
