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.