Top
Previous Next
Java Arrays, Objects, Methods CS 161 - Java

Creating an array

... is like creating an object from a class: Arrays of primitive data types are initialized to 0
int[] grades;

grades = new int[60];

Arrays of Objects are initialized to null

Student[] students;

students = new Student[60];

The students array has been declared and instantiated, but not yet initialized: no Student object references have been assigned to the array elements.

To initialize the array elements, you need to instantiate each individually:

for (int nextStudent = 0; nextStudent < 10; nextStudent++ ) {
   students[nextStudent] = new CSStudent();
}

Top
Previous Next
jwd