Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 2

Working with many objects

Arrays of objects, and objects that hold other objects

18m50 XP
→→→

What you'll be able to do

  • Create an array of objects and fill it correctly
  • Loop over objects and call their behaviour
  • Explain why `new Student[3]` contains no students at all
  • Write a class whose job is to manage other objects
1

Multiple Objects

Arrays of objects: two steps, not one

`new Student[3]` creates three *slots*, not three students. Forgetting this is the classic NullPointerException.

Filling an array of objects

Main.java
1Student[] students = new Student[2];
2students[0] = new Student();
3students[0].name = "Sara";
4students[1] = new Student();
5students[1].name = "Omar";
Student[] students = new Student[2];
Creates the array object with two slots, each holding null. No Student exists yet.
students[0] = new Student();
*Now* a Student is created and its reference is stored in slot 0.
students[0].name = "Sara";
Two dots' worth of work: find the object in slot 0, then set its field.
students[1] = new Student();
Each slot needs its own new — the array does not create anything for you.

An array of objects starts full of nulls

new Student[3] then students[0].name throws NullPointerException, because slot 0 holds null. Rule of thumb: one new for the array, plus one new per element.

A class that manages other objects

Once you can hold many objects, you can write a class whose whole job is to coordinate them: a Classroom that counts passing students, a Bank that sums balances. That is your first taste of HAS-A (Module 12) and it is exactly how the final project is structured: Bank holds Account[], each Account holds a Customer.

Count the passing students

Add a fourth student with grade 49 and re-run. Then move the `isPassing()` rule to `grade >= 60` and see the report change in one place only.

Output
Press Run to execute this example.

Prove it — coding challenges

Common mistakes

WrongStudent[] list = new Student[3]; list[0].name = "Sara";
WhySlot 0 still holds `null`, so the dot has nothing to follow.
Fixlist[0] = new Student(); before touching its fields.
Wrongfor (int i = 0; i <= list.length; i++)
WhyOne index too far — ArrayIndexOutOfBoundsException.
FixUse `i < list.length` or a for-each loop.
WrongDuplicating the passing rule inside every loop that walks students.
WhyWhen the rule changes you must find every copy — the Module 1 problem returning.
FixPut the rule in the object: `boolean isPassing()` and call it everywhere.

Lesson playground

Free practice

A Classroom object that owns its students. Add a method that returns the highest grade.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

This code throws NullPointerException on the third line. Why?

Main.java
1Student[] students = new Student[2];
2students[0] = new Student();
3students[1].name = "Omar";

Summary

  • `new Type[n]` creates n null slots; every element still needs its own `new`.
  • for-each is the safest way to walk objects and call their behaviour.
  • A class can hold other objects — the beginning of HAS-A design.
  • Rules that depend on an object's state belong inside that object as a method.