How to override equals method?

4

I'm trying to override the equals method for instead of validating if an integer object is equal to the other just checking if a value in this object is the same as the other.

Real Scenery: Object Student being him. name and rg, and age if rg is the same meaning 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 having a hard time getting the "this" which is the value of the object before .equals.

    
asked by anonymous 12.04.2016 / 21:06

1 answer

5

You have two problems with your code:

  • You have defined static variables for your Student class, meaning that all objects in this class will share the same values for the variables. Remove% with% of them that is already for your code to work properly;

  • Your static method is not correctly overriding the equals() method of the Object class, although it would also work because you are making an explicit call to the equals() method of the Student class, but it can be confusing.

    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 the Ideone .

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

      

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

    For the case in question this can be left out because hashCode() no longer follows the agreement 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 because I think it goes a little way from the scope of the question, a more detailed answer on the subject can be found here: What is the importance of implementing the hashCode method in Java?

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

        
    12.04.2016 / 21:15