The Person blueprint
An array of objects does not hold objects: it holds references to them. The Person class is the blueprint: name, age, a constructor, and getName().
An array of objects does not hold objects: it holds references to them.
The Person class is the blueprint: name, age, a constructor, and getName().
An array of objects does not hold objects: it holds references to them. The Person class is the blueprint: name, age, a constructor, and getName().
The light blue particle is the execution flow. In main it runs new Person[3].
That reserves three slots able to point to a Person, but it builds none of them.
All three slots are born null: three envelopes, no letters.
The light blue particle is the execution flow. In main it runs new Person[3]. That reserves three slots able to point to a Person, but it builds none of them. All three slots are born null: three envelopes, no letters.
What happens if we call team[0].getName() right after creating the array?
The flow reads slot [0], finds null, and it points to no object at all.
The call fails with NullPointerException: the most common Java error.
What happens if we call team[0].getName() right after creating the array? The flow reads slot [0], finds null, and it points to no object at all. The call fails with NullPointerException: the most common Java error.
We fix line 4 with the second step: team[0] = new Person("Laura", 28).
The real object is born on the Heap and slot [0] stores the reference to it.
We fix line 4 with the second step: team[0] = new Person("Laura", 28). The real object is born on the Heap and slot [0] stores the reference to it.
On line 5, team[1] gets its own new Person("Carlos", 35).
Slot [2] stays unassigned: it is still null.
On line 5, team[1] gets its own new Person("Carlos", 35). Slot [2] stays unassigned: it is still null.
for (Person p : team) walks the array; if (p != null) guards each pass.
At [0] and [1] the check passes: we follow the arrow and print the name.
At [2], p != null is false: the slot is skipped, no NullPointerException.
for (Person p : team) walks the array; if (p != null) guards each pass. At [0] and [1] the check passes: we follow the arrow and print the name. At [2], p != null is false: the slot is skipped, no NullPointerException.
Now team[2] = team[0] copies the arrow of slot [0], not the object.
Both slots point to the same object: team[0] == team[2] is true.
Now team[2] = team[0] copies the arrow of slot [0], not the object. Both slots point to the same object: team[0] == team[2] is true.
Creating an array of objects takes two steps: new Person[3] and each new Person(...).
The first creates null slots; the second builds the objects on the Heap.
If the array can have gaps, check p != null before calling a method on it.
Creating an array of objects takes two steps: new Person[3] and each new Person(...). The first creates null slots; the second builds the objects on the Heap. If the array can have gaps, check p != null before calling a method on it.