|
Java Arrays, Objects, Methods
|
CS 161 - Java |
... is like creating an object from a class:
- declaration - assigns a name to the reference
- instantiation - creates space for the object
- initialization - gives the objects values for the first time
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();
}