0

I am a Java beginner and I was trying to implement an equals method in an Address Class that I have made.

public class Address {
protected String validFrom;
protected String validTo;

 public boolean equals(Object otherObject) {
    if(otherObject == null)
        return false;
    else if
        (getClass( ) != otherObject.getClass( ))
        return false;
    else
        Address otherAddress = (Address) otherObject;
        return (validFrom.equals(otherAddress.validFrom) && validTo.equals(otherAddress.validTo));
}

However I am getting an error at

Address otherAddress = (Address) otherObject;

The error is :

Address cannot be resolved to a variable

  • 1
    Use curly braces in your if else statements and it will be ok. Your return statements in `else if` and `else` block are out of scope. – Tarmo Mar 05 '21 at 12:09
  • while this doesn't answer the question, here's a better equals() implemenation for you: [ta-dah](https://pastebin.com/cBa0zXik) - better because if yours will throw NPE if `validTo` or `validFrom` are null when calling `.equals()` on a non-null Address object. – Shark Mar 05 '21 at 14:52

0 Answers0