'how to override the equals() method when you have to compare 7 instance variables? [duplicate]

I am currently doing my programming assignment. But there is one step that i don't quite understand. There are 7 private instance variables and we are supposed to use the equals() method to compare all the instance variables. How is that possible? This is the question- The class has to override Object’s equals() method in order to return true if the object as argument equal to the object who invoked the method and false otherwise. The method should compare all instance variables of the two objects.

And these are the instance variables-

public class Book implements Usable{
//instance variables


 private String b1, b2, b3, b4, b5, b6, b7; 
 private boolean bookReference;


Solution 1:[1]

Not sure what you mean... are you supposed to use the String's equal method for the 7 variables? In that case, here's an idea... you do need to check for null, though. If you can use another library, you can use StringUtils.equal() instead of as that checks for null as well. You should also implement hashCode() as well for correctness.

@Override
public boolean equals (Object o) {

    if (o == this)
      return true;
    if (!(o instanceof Book))
      return false;

    Book that = (Book) o;

    if (!this.b1.equals(that.b1)) return false;
    if (!this.b2.equals(that.b2)) return false;
    //Add the rest of the variable

    return true;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xxx