How to override equals method?

Asked

Viewed 3,440 times

4

I am trying to overwrite the equals method for instead of validating if an entire object is equal to the other just check if a value this object is the same as the other.

Cenário Real: Student object being it. name and rg, and age if the rg is equal means that they are the same person.

Pupil 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 "this" which is the value of the object before the . equals.

  • You want to overwrite the equals() who inherits from the Object, right?

1 answer

5


You have two problems with your code:

  1. 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 the static of which it is already for your code to work properly;

  2. His method equals() is not correctly overwriting the method equals() of the Object class, although it would also work because you are making an explicit call to the method equals() class Student, 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 on Ideone.

As a general rule, according to the documentation Java, whenever override the method equals() 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 one can leave it aside because the 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 didn’t put that in the code because I think it’s a little outside the scope of the question, a more detailed answer on the subject can be found here: How important it is to implement the hashcode method in Java?

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

  • 1

    this Static in my life is worse than ;, mass is that Casting loco of Object

  • do only public boolean equals(Aluno a) {
 boolean result = false;
 if (this.getRg() == a.getRg()) {
 result = true;
 }
 return result;
 } works, has problem ?

  • 1

    No problem, no problem, but maybe it would be a good idea to change the name of the method equals() for some other so you do not confuse with the equals() of the Object class. But since it seems to me to be a small code I would say that it is not quite essential. If you do so it will also work.

  • I just got an update on the answer, if you want to take a look.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.