Concepts / Creating and Filling an Array of Objects

Creating and Filling an Array of Objects

Once an array of objects is filled, classlist[i] refers to a Student object, and it can be used anywhere a Student variable could be used.

  • Programming

A Class Stored Under One Name

Imagine a teacher who keeps track of a class of students, where each student is represented by a Student object. Storing those objects in an array lets a program hold the whole class under one name, such as classlist. That solves the first problem, but it raises a harder one: once a particular student is sitting inside that array, how do you reach them? This article follows a single array, classlist, and shows exactly what happens when you ask it for one of its students, what you can ask that student to do, and where learners most often go wrong.

What Sits in Each Slot

An array of objects has a numbered row of slots, and each slot is reached by an index written in square brackets. Before the array is filled, the slots do not yet hold students. Once every slot has been filled with a Student object, the expression classlist[i] stands for the Student that lives at position i. The brackets select the slot, and the slot leads you to the object.

Read the diagram from left to right. Each box is one slot of the array, labeled with its index, and each slot holds a Student object. The highlighted box is classlist[3]. The index counts from zero in Java, so the box labeled 3 is the fourth slot of the array. Everyday speech may call this student number 3, but the index used in code is the position counted from zero. This five-slot array is an illustration chosen for this article, not a fixed size from the source.

Why classlist[i] Acts Like a Student

Definition

Once an array of objects is filled, classlist[i] refers to a Student object, and it can be used anywhere a Student variable could be used.

The reason this holds is that the array is only a way of naming the object. Once the index has selected a slot, the object in that slot behaves exactly as it always does. The source makes this point directly: you can do anything with classlist[i] that you could do with any other variable of type Student. That includes reading properties, calling methods, and passing the object to other code. So classlist[i] is not a special kind of value that needs its own rules. It is a Student, reached by a different route.

Key point

The brackets choose which object you are talking about. Everything after the brackets is then handled by that Student object, just as it would be if you had stored the same student in an ordinary variable.

Reaching a Property Through an Index

A property belongs to the object, not to the array. To reach a property through the array, first select the object with the index, then attach the property name with a dot. The pattern is always the array name, then the index in brackets, then a dot and the property name. The source gives the form classlist[i].name for the name of the ith Student, and the specific case classlist[3].name for the name of the Student stored at index 3.

java
Notes on the code
  • classlist selects the array
  • [3] selects the Student at index 3, counted from zero
  • .name reads the name property of that Student
Worked example

Reading one student's name from a filled class array

The array classlist has been filled with Student objects. Get the name of the Student stored at index 3.

  1. 1
    Identify the array

    The array is named classlist, so the expression begins with classlist.

  2. 2
    Select the slot

    Index 3 is written in brackets, giving classlist[3]. Because Java counts from zero, this is the fourth slot.

  3. 3
    Treat the selection as a Student

    Since the slot is filled, classlist[3] can be used like any Student variable.

  4. 4
    Attach the property

    Adding .name gives classlist[3].name, which reads the name property of that object.

Result

classlist[3].name is the name of the Student stored at index 3.

Calling a Method on an Array Element

Methods are reached the same way as properties. You select the object with the index, then use the dot to name the method and add parentheses for the call. The source gives classlist[i].getAverage() as the way to compute the average for student number i. Because the index picks out an object, the method call runs on that object and uses that object's own data.

java
Notes on the code
  • classlist[i] selects the Student at position i
  • .getAverage() invokes that Student's method
  • the parentheses make it a call rather than a property read
FormWhat the dot reachesParenthesesExample
Property accessA stored property of the StudentNoneclasslist[i].name
Method invocationA method that runs on the StudentYes, the callclasslist[i].getAverage()

Both forms use the same index-then-dot step; only the thing after the dot differs.

The same index-then-dot step works for both forms because the index has already done its job by the time the dot appears. It has chosen one Student. The dot then asks that Student for something, whether a property value or a method result. Calling getAverage() on classlist[2] and calling it on classlist[4] therefore usually give different results, since each Student carries different data.

What do you think happens?

Is classlist[3] a Student, or is it something else that only holds a Student?

  • It is a Student, so it can be used anywhere a Student variable could be used

  • It is only a reference slot, so it cannot have properties or methods

Reveal answer
Answer:

It is a Student, so it can be used anywhere a Student variable could be used.

The source states that once the array is filled, classlist[i] refers to a Student object, which can be used like any Student variable, including reading properties and calling methods.

Mistakes Beginners Make with Index Access

  • Common mistake

    Thinking the array holds the property directly

    What it looks like

    Writing classlist.name when you want one student's name

    Why it fails

    A property belongs to the object, not the array. The array name alone does not select a student.

    Do this instead

    Select the object first with an index, as in classlist[3].name.

  • Common mistake

    Counting the index from one

    What it looks like

    Using classlist[3] and expecting the third student

    Why it fails

    Java array indices start at zero, so index 3 is the fourth slot.

    Do this instead

    Count from zero when choosing the index, or describe the position as the fourth slot.

  • Common mistake

    Dropping the dot or the parentheses

    What it looks like
    Writing classlist[i]getAverage() or classlist[i].getAverage
    Why it fails

    The dot connects the selected object to the property or method, and the parentheses mark a method call.

    Do this instead

    Write the full pattern, such as classlist[i].getAverage().

  • Common mistake

    Treating classlist[i] as a different kind of value

    Why it fails

    The source says classlist[i] can be used anywhere a Student variable could be used.

    Do this instead

    Reason about it as a Student, reading properties and calling methods the same way you would on a standalone variable.

Practice Reaching Students

EASY

The array classlist has been filled with Student objects. Write an expression that reads the name of the Student at index i, and another that calls getAverage() on the Student at index 2. Explain in one sentence why both expressions begin with the same classlist[index] step.

Hints
  • Start with the array name and the index in brackets.
  • Use a dot after the brackets, then the property name or the method name.
  • The brackets select the object; the dot then asks that object for something.

Key Takeaways

  1. 1
    Once an array of objects is filled, classlist[i] refers to a Student object.
  2. 2
    classlist[i] can be used anywhere a Student variable could be used.
  3. 3
    Properties are reached with the array name, the index in brackets, then a dot and the property name, as in classlist[i].name.
  4. 4
    Methods use the same index-then-dot pattern, as in classlist[i].getAverage(), and run on the object the index selects.

At a glance

  • Once an array of objects is filled, classlist[i] refers to a Student object.
  • The brackets select the slot and the slot leads to the object, so classlist[i] behaves like any Student variable.
  • Properties and methods are both reached with index then dot, as in classlist[i].name and classlist[i].getAverage().
  • Java indices start at zero, so index 3 is the fourth slot.