title: Printing arrays - overriding toString() method
body: <p>I have two different class, Student and StudentTest - the Student class has;</p>
  
  <pre><code>public class Student {
  
  // Data Members 
  private String name; // The name of this student
  private long idNumber; // The ID number of this student
  
  // Constructs a new Student with passed name and ID number parameters.
  public Student(String studentName, long studentIDNumber) {
  name = studentName;
  idNumber = studentIDNumber;
  }
  
  // Returns the name of this student.
  public String getName() {
  return name;
  }
  
  // Returns the ID number of this student.
  public long getIDNumber() {
  return idNumber;
  }
  
  // Sets the name of this student.
  public void setName(String studentName) {
  name = studentName;
  }
  
  // Sets the ID number of this student.
  public void setIDNumber(long studentIDNumber) {
  idNumber = studentIDNumber;
  }
  
  @Override
  public String toString(){
      return "Name: " + this.name;
  }
  
  } // end class
  </code></pre>
  
  <p>And the StudentTest class has 3 different methods, 1. to ask the user to enter the size of an array and then create an array of type Student, 2. to ask the user to populate the array with names and ID numbers, 3. to display the contents of the array.</p>
  
  <pre><code>import java.util.Scanner; 
  
  public class StudentTest { 
  
  // Main method.
  public static void main(String [] args) {
  
      Student[] students = createArray();
      populateArray(students);
      displayArray(students);
  }
  
  // Method that asks user for size of array.
  public static Student[] createArray() {
  
  System.out.println("Enter size of array: ");
  Scanner userInputEntry = new Scanner(System.in);
  int inputLength = userInputEntry.nextInt();
  Student students[] = new Student[inputLength];
  
  return students; 
  
  }
  
  // Method that asks user to populate array.
  public static void populateArray(Student [] array) {
  
      for(int i = 0; i &lt; array.length; i++) {
          array[i] = new Student();
          System.out.println("Enter student name: ");
          Scanner userInputEntry = new Scanner(System.in);
          array[i].setName(userInputEntry.next());
          System.out.println("Enter student ID number: ");
          array[i].setIDNumber(userInputEntry.nextLong());
      }
  
  }
  
  // Method that displays contents of array.
  public static void displayArray(Student[] array) {
  
      for(int i = 0; i &lt; array.length; i++){
          System.out.println(array[i].toString());
      }
  
  }
  
  }
  </code></pre>
  
  <p>When I try to run it, I get an error about the</p>
  
  <pre><code> array[i] = new Student();
  </code></pre>
  
  <p>in the for loop in the second method.</p>