Concepts / Creating and Using Objects in Java

Creating and Using Objects in Java

Declaring an array of objects uses the class name as the base type, such as Student[] classlist.

  • Programming

Predicting What an Object Array Contains

Many learners who first meet arrays of objects expect a single statement to do all the work. They assume that writing an array declaration and a creation statement will produce a set of ready-to-use objects. This is the most common way this topic goes wrong. The goal of this article is to trace what the program actually stores at each step, so that you can see where the objects come from and where they are missing.

What do you think happens?

After Student[] classlist = new Student[30]; how many Student objects exist, and what does classlist[0] contain?

  • Thirty Student objects, each ready to use

  • No Student objects, and classlist[0] is null

  • One Student object, shared by all thirty slots

Reveal answer
Answer:

No Student objects exist, and classlist[0] is null.

Creating the array allocates thirty slots and sets each one to null. The array creation does not create any Student objects. Each object must be created explicitly.

Declaring the Array Variable

An array's base type can be a class, not only a primitive type such as int or a type such as String. Declaring an array of objects uses the class name followed by square brackets as the type. In the declaration Student[] classlist, the part Student sets the type of each element. Because the base type is Student, every element of classlist is a variable whose type is Student.

A variable of type Student is a place that can hold a reference to a Student object. It is not the object itself. This is the key distinction in the whole topic. The declaration introduces a variable that is able to refer to an array of Student values. It does not create the array, and it does not create any students.

java
Notes on the code
  • Student is the base type: each element will be a Student reference
  • The brackets mark this variable as an array
  • classlist is declared but has no array allocated yet

Creating the Slots and Leaving Them Empty

Creating the array uses the new keyword together with the base type and a size. The statement new Student[30] allocates thirty elements, and each one starts as null. A null element means the slot exists but refers to nothing. The array has thirty places ready for students, but no student has been made yet.

java
Output
No output. classlist now refers to an array of 30 slots, each holding null.
Notes on the code
  • Declares the variable and allocates 30 Student slots in one statement
  • Each slot is null after this line runs
  • No Student object has been created by this statement
classlistStudent[] variableslot 0nullslot 1nullslot 2nullindex 0index 1index 2
After Student[] classlist = new Student[3]; what is stored in each slot, and where are the Student objects? Each slot holds null, and no Student objects exist yet.

The diagram shows the state the array is in before any objects are assigned. The variable classlist refers to the container, and each slot inside it is still empty. The container and the objects it is meant to hold are separate things, and this separation is the central idea of the topic. The array creation statement stops at null.

Filling Each Slot With a New Object

To create the actual objects, the program must visit each index and assign a new object to it. A for loop does this. The loop variable i takes the values 0 through 29, and on each pass the statement creates one Student and stores it in the slot at that index. The loop is the part that turns thirty nulls into thirty objects.

java
Output
No printed output. After the loop, classlist[0] through classlist[29] each refer to a separate Student object.
Walk through it
  1. 1
    Line 1 creates the array; every slot starts as null.
  2. 2
    Line 2 starts the loop with i equal to 0.
  3. 3
    Line 3 creates a new Student and assigns it to classlist[i], replacing null in that slot.
  4. 4
    Line 4 closes the loop body, then i increases by one and the loop repeats until i reaches 30.
Notes on the code
  • Line 1 allocates 30 empty slots
  • The loop variable i runs from 0 up to 29
  • Each pass creates one new Student and stores it at index i
Startall slots nulli = 0slot 0 gets a Studenti = 1slot 1 gets a Studenti = 2slot 2 gets a Studenti = 3loop stopsfirst passi increasesi increasescondition false
As the loop runs, how does each slot change from empty to holding its own Student object? Each pass fills exactly one index, and the earlier slots keep their objects.
Worked example

Filling a three-slot class list

Trace what the program holds after Student[] classlist = new Student[3]; followed by a loop that runs from i = 0 to i = 2 and assigns classlist[i] = new Student();

  1. 1
    Create the array

    Three slots are allocated: classlist[0], classlist[1], and classlist[2]. Each holds null.

  2. 2
    Pass with i = 0

    A new Student is created and stored in classlist[0]. Slots 1 and 2 are still null.

  3. 3
    Pass with i = 1

    A second, separate Student is created and stored in classlist[1]. Slot 0 keeps its original object.

  4. 4
    Pass with i = 2

    A third Student is stored in classlist[2]. The loop then stops because i has reached 3.

Result

The array holds three different Student objects, one in each slot. No slot is null.

Where an Unfilled Slot Causes a Failure

If a loop is skipped, shortened, or written with the wrong bound, some slots remain null. The program does not fail when the array is created, and it does not fail when the loop runs. It fails at the statement that tries to use an element that was never assigned. At that point the program is asking a null slot to act like a Student, and there is no object there to respond.

Programrunning codeclasslist[1]never assignedStudent methodneeds an objectread slot 1returns nullcall on null: run fails
Which statement fails when an element was never assigned, and what does the program try to do at that point? The read of classlist[1] returns null, and the later call fails because there is no object behind it.

Mistakes Beginners Make With Object Arrays

  • Common mistake

    Assuming new Student[30] creates thirty Student objects

    What it looks like

    Student[] classlist = new Student[30]; then using classlist[0] directly

    Why it fails

    The array creation allocates thirty slots, each set to null. No Student objects are created by this statement.

    Do this instead
    Create each object explicitly, for example with a loop that assigns classlist[i] = new Student();
  • Common mistake

    Treating the element type as the array type

    What it looks like

    Thinking that Student[] classlist makes classlist itself a Student

    Why it fails

    The base type Student describes each element. classlist is an array of Student references, not a single Student.

    Do this instead

    Read Student[] as an array whose elements are Student references.

  • Common mistake

    Writing a loop bound that misses a slot

    What it looks like
    for (int i = 0; i < 29; i++) leaves classlist[29] as null
    Why it fails

    The loop variable must cover every index from 0 through 29 for the array to be fully filled.

    Do this instead

    Use the bound that matches the array size, such as i < 30 for an array of thirty elements.

  • Common mistake

    Forgetting that the object creation belongs inside the loop body rather than before it

Practice Tracing the Object Array

MEDIUM

A program declares Student[] classlist = new Student[4]; and then runs a loop with i from 0 to 1, assigning classlist[i] = new Student(); Which slots hold Student objects, which hold null, and which statement fails if the program later calls a method on classlist[3]?

Hints
  • Ask first which statement creates the objects, and which only creates the slots.
  • Check the loop's last value of i against the index of each slot.
  • A method call needs an object behind the reference; a null reference has none.

Key Takeaways

  • Declaring Student[] classlist uses the class name as the base type, so each element is a variable that can hold a reference to a Student.
  • Creating the array with new Student[30] allocates thirty slots, and each one starts as null; no Student objects are created by that statement.
  • A for loop that assigns classlist[i] = new Student() for each index fills the array with real, separate objects.
  • An unfilled slot causes a failure at the statement that tries to use it, so trace which index the loop actually assigned when debugging.