Suppose, I have a Class, call it Student. The student class has an element, an int called Id. I want to override equals, so that if a Student is compared to an Integer the method returns true. like:
public class OverrideTest {
public static void main(String[] args) {
Student a = new Student();
a.setId(5);
System.out.println(a.equals(5));
}
public static class Student {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (o instanceof Student) {
Student a = (Student) o;
if (a.getId() == this.getId())
return true;
}
if (o == this)
return true;
if (o instanceof Integer) {
int id = (Integer) o;
if (id == this.getId())
return true;
}
return false;
}
}
}
Is there a way to signal the IDE that this is okay and no warnings have to be sent?
This returns true, but with a syntax warning in IDE.