title: Setter sets value for every object in class
body: <p>I have a program that takes students' names and grades as user input and then performs some operations on them, which are irrelevant for the scope of the question. The code is as follows:</p>
  
  <pre><code>import java.io.*;
  import java.util.Arrays;
  import java.util.Scanner;
  
  public class Student {
  // Four attributes that define Student
  private String name;
  private double points;
  private int startYear;
  private int[] grades;
  
  public Student(String name, double points, int startYear, int[] grades) {
      this.name = name;
      this.points = points;
      this.startYear = startYear;
      this.grades = grades;
  }
  //Constructor. Everyone starts with 0 points and this year
  
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in); //Create scanner
      System.out.println("Please enter the number of students:");
      int count = sc.nextInt(); // Number of students
      System.out.println("Please enter the number of grades:");
      int count1 = sc.nextInt(); // Number of grades
      Student students[] = new Student[count]; // Create array of student objects based on previously entered value
      int[] temp = new int[count1]; // Temporary array for storing grades entered
      for (int i = 1; i &lt; count + 1; i++) {
          System.out.println("Please enter the name of student " + i);
          String name = sc.next();
          students[i - 1] = new Student(name,0.0,2018,temp); // Creating student object
          System.out.println("Please enter grades of  " + name);
          for (int k = 0; k &lt; count1; k++) {
              int personal_grades = sc.nextInt();
              temp[k] = personal_grades; //filling the temporary array
              //System.out.println(grades.length); //for debugging
  
          }
          students[i - 1].setGrades(temp); //transferring from temporary array to student object array
          students[i-1].printGrades();
  
      }
      System.out.println((students[0].name));
      System.out.println((students[1].name));
      System.out.println(Arrays.toString(students[0].grades));
      System.out.println(Arrays.toString(students[1].grades));
      for(int i = 0; i &lt; count; i++) {
          System.out.println("Grades of  " + students[i].name + " are:");
          //students[i].printGrades();
      }
      for(int i = 0; i &lt; count; i++) {
          System.out.println("Average of  " + students[i].name + " is:");
         // students[i].average();
      }
      int passed=0;
      for(int i = 0; i &lt; count; i++) {
  
          if(students[i].average()&gt;5.5)
          {
              passed++;
  
          }
  
      }
      System.out.println(passed+" students passed!");
  
  
  }
  
  public void setGrades(int[] temp) {
  
          this.grades = temp;
  
  
  }
  
  public int[] getGrades() {
      return grades;
  }
  
  public void printGrades() {
  
  
          System.out.println(Arrays.toString(grades));
  
      }
  
  public float average (){
      int k = 0;
      int sum=0;
      float average=0;
      while (k &lt; this.grades.length) {
          sum=sum+this.grades[k];
         k++;
  
      }
      average = sum/(float)this.grades.length;
      System.out.println(average);
      return average;
  }
  }
  </code></pre>
  
  <p>The problem I am having with the code is as follows: the setter method appears to set the values for all of the objects that were ever created. Take this test run as an example:
  <a href="https://i.stack.imgur.com/mYgrS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mYgrS.png" alt="enter image description here"></a></p>
  
  <p>You can see that the grades for the last student entered appear in every student's record. I have debugged and found that it is the <code>setGrades</code> method that causes this. However, I am using the <code>this</code> keyword - why does it set the value for all the objects then?</p>