📘LESSON 5
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define arrays of objects in Java.
- Create objects and store them in arrays.
- Access object properties from arrays.
- Process arrays of objects using loops.
- Explain the importance of arrays of objects in software development.
3.2 Overview
Arrays in Java are not limited to primitive data types only. Java also allows developers to store objects inside arrays. Arrays of objects are commonly used when applications need to manage groups of related entities such as students, employees, products, or customers.
This lesson introduces learners to arrays of objects and explains how object references are stored and managed within arrays. Learners will also explore how arrays of objects support data organization and simplify application development.
Arrays of objects are widely used in:
- banking systems,
- inventory systems,
- student management systems,
- healthcare applications,
- and enterprise software solutions.
Understanding arrays of objects is essential because modern Java applications frequently process large collections of related objects.
KT0501 — Introduction to Arrays of Objects
An array of objects is an array that stores object references instead of primitive values.
Each element in the array refers to an object created from a class.
Example Structure
Student[] learners;
In this example:
- Student is a class,
- learners is an array storing Student objects.
Difference Between Primitive Arrays and Object Arrays
| Primitive Arrays | Object Arrays |
|---|---|
| Store values directly | Store object references |
| Store simple data | Store complex data |
| Example: int[] | Example: Student[] |
Importance of Arrays of Objects
Arrays of objects help developers:
- manage grouped objects efficiently,
- organize application data,
- simplify object processing,
- and model real-world systems.
KT0502 — Creating a Class for Object Arrays
Before creating arrays of objects, a class must first be defined.
Example Class
public class Student {
String name;
int marks;
}
Explanation
| Component | Description |
|---|---|
| String name | Stores student name |
| int marks | Stores student marks |
KT0503 — Creating Objects in Arrays
Objects can be created and stored inside arrays.
Example
public class Main {
public static void main(String[] args) {
Student[] learners = new Student[2];
learners[0] = new Student();
learners[1] = new Student();
learners[0].name = "John";
learners[0].marks = 80;
learners[1].name = "Sarah";
learners[1].marks = 90;
System.out.println(learners[0].name);
System.out.println(learners[1].name);
}
}
Output
John
Sarah
Explanation of the Process
| Step | Description |
|---|---|
| Create array | Stores object references |
| Create objects | Objects created using new keyword |
| Assign values | Store data in object properties |
| Access objects | Retrieve object data using indexes |
KT0504 — Accessing Object Properties in Arrays
Object properties are accessed using:
- array indexes,
- followed by the dot operator.
Example
System.out.println(learners[0].marks);
Output
80
Importance of the Dot Operator
The dot operator allows access to:
- variables,
- methods,
- and object behaviour.
KT0505 — Looping Through Arrays of Objects
Loops are commonly used to process arrays of objects.
Example Using a for Loop
public class Main {
public static void main(String[] args) {
Student[] learners = new Student[2];
learners[0] = new Student();
learners[1] = new Student();
learners[0].name = "John";
learners[1].name = "Sarah";
for(int i = 0; i < learners.length; i++) {
System.out.println(learners[i].name);
}
}
}
Output
John
Sarah
Enhanced for Loop Example
for(Student learner : learners) {
System.out.println(learner.name);
}
Benefits of Looping Through Object Arrays
Looping allows developers to:
- process multiple objects efficiently,
- reduce repetitive code,
- and simplify application logic.
KT0506 — Real-World Example of Arrays of Objects
A school management system may store:
- student objects,
- teacher objects,
- or subject objects
inside arrays for easier management.
Example:
- Student[] learners
- Product[] products
- Employee[] staff
Arrays of objects help organize application data into manageable structures.
3.5 Key Notes / Summary
- Arrays of objects store object references.
- Objects must be created before storing data.
- Classes define the structure of objects.
- Object properties are accessed using indexes and the dot operator.
- Loops simplify processing arrays of objects.
- Arrays of objects are commonly used in real-world software systems.