How to overwrite equals method?

I'm trying to overwrite the equals method to instead validate if one Integer object is equal to the other just check if a value in this object is equal to the other.

Real Scenario: Student object being him. name and rg, and age if the rg is equal means they are the same person.

Student Class:

public class Aluno {

   private static String nome;
   private static float rg;
   private static int idade;

   public Aluno(String nome, float rg, int idade) {
      this.nome = nome;
      this.rg = rg;
      this.idade = idade;
   }

   public boolean equals(Aluno a) {
      boolean result = false;
      if (this.getRg() == a.getRg()) {
         result = true;
      }
      return result;
   }   

   // getters and setters

}

Main Class:

public class main {

   public static void main(String[] args) {
      Aluno a1 = new Aluno("A", 12, 20);
      Aluno a2 = new Aluno("B", 11, 25);
      Aluno a3 = new Aluno("A", 25, 28);
      Aluno a4 = new Aluno("D", 12, 21);
      System.out.println(a1.equals(a3)); // aqui deveria retornar false
      System.out.println(a1.equals(a4)); // aqui deveria retornar true
   }
}

Apparently I'm making a mess of taking the "this" which is the value of object before the .equals.

Author: Maniero, 2016-04-12

1 answers

Has two problems in your code:

  1. You have defined static variables for your student class, meaning that all objects in that class will share the same values for the variables. Remove the static from them that is already for your code to work properly;

  2. Your equals() method is not correctly overwriting the equals() method of the Object class, although it would also work as you are making an explicit call to the equals() method of the object class. student class, but can get confused.

The code below runs correctly:

class Aluno {
   private String nome;
   private float rg;
   private int idade;

   public Aluno(String nome, float rg, int idade) {
      this.nome = nome;
      this.rg = rg;
      this.idade = idade;
   }

   @Override
   public boolean equals(Object o) {
      boolean result = false;
      if (this.getRg() == ((Aluno)o).getRg()) {
         result = true;
      }
      return result;
   }   

   public float getRg() {
     return this.rg;
   }
}

class Ideone
{
    public static void main (String[] args)
    {
      Aluno a1 = new Aluno("A", 12, 20);
      Aluno a2 = new Aluno("B", 11, 25);
      Aluno a3 = new Aluno("A", 25, 28);
      Aluno a4 = new Aluno("D", 12, 21);
      System.out.println(a1.equals(a3)); // aqui retorna false
      System.out.println(a1.equals(a4)); // aqui retorna true
    }
}

See in Ideone .

As a general rule, according to the Java documentation, whenever you overwrite the equals() method you should also overwrite the hashCode(). Original text in English:

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

For the case in question you can leave this aside because equals() no longer follows the contract established in the documentation since it should for example check if the variable contains null reference or if it references an object of another type. I did not put this in the code as I think it runs a bit out of the scope of the question, a more detailed answer on the subject can be found here: What the importance of implementing hashCode method in Java?

For the case in question, however, you can keep knowing the technical debt that your code has, or you can still change the name of the method equals() to some other, if you want to make it even clearer that it is not the method equals() of the Object class.

 5
Author: Math, 2017-04-13 12:59:42