0

I have an unexpected and confusing issue with the instance comparing.

So, by order:

I try to compare two instance of the same class and override the hashcode() and equals() methods:

public class AdminUser {
.............
  @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }

        if(obj == null){
            return false;
        }

        Class c1 = obj.getClass(); //-----------{1}
        Class c2 = this.getClass(); //----------{2}


        if (this.getClass() != obj.getClass()){
            return false;
        }

        AdminUser other = (AdminUser)obj;
        return Objects.equals(this.username, other.username) &&
                Objects.equals(this.username, other.username) &&
                Objects.equals(this.username, other.username);
    }
}
  • The method, from where I compare:
public class AdminCRUD extends BaseWithPerformanceTest {
    private void assertIfUserExist(AdminUser cau) {
        AdminUser createdUser = createUserForm.getUserFromForm();  //----{3}
        if(!cau.equals(createdUser)){
            saveScreenshot("Users_dont_match");
        }
    }
}
  • The instance where I take AdminUser ({3}):
public class UsersPage extends TopBarConsoleMenu {
..................
    public class CreateUserForm extends BaseForm {
        public AdminUser getUserFromForm() {
            ......................

            return new AdminUser(){{
                setEmail(ACTUAL_EMAIL);
                setName(ACTUAL_NAME);
                setUsername(ACTUAL_USER_NAME);
                setUserType(EnumUtils.getEnumByMethodName(AdminUserType.class, "getAddUserDropDownValue", ACTUAL_USER_TYPE));
                setRegion(EnumUtils.getEnumByMethodName(AdminRegion.class, "getDropdownValue", ACTUAL_REGION));
            }};
        }
    }
}

So the issue:

(please see AdminUser class)

  • In the point {1}, c1 variable got the pages.admin.UsersPage$CreateUserForm$1 instance!
  • In the point {2} c2 variable as expected utils.data.admin.AdminUser$1

Except other - the obj instance, actually contains correct instance! - After boxing AdminUser other = (AdminUser)obj; I can get any correct data from the other variable - there are really AdminUser instance!

What are happens?

Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59
  • 3
    By using the `{{`-"trick" (called double brace initialization) you're actually implementing an anonymous subclass of `AdminUser`. – Joachim Sauer Apr 30 '21 at 14:37
  • @JoachimSauer thanks, where I can research the mechanism of this "trick" to understanding why it happens? Or could you can explain the issue in the answer? – Valentyn Hruzytskyi Apr 30 '21 at 14:44
  • 1
    There's a link to another question on top of your question, it has tons of details. – Joachim Sauer Apr 30 '21 at 14:45
  • How can I know this issue occurred by "Double Brace initialization"? Why someone degrees my question? This question isn't duplicate. – Valentyn Hruzytskyi Jul 05 '21 at 10:03
  • You asked "what happened". What happened is that you created a subclass of `AdminUser` via the double-brace initialization and therefore comparing the `getClass()` return value will not be the same as a "normal" `new AdminUser` and therefore your `equals()` method will return `false`. The duplicate explains what the double-brace initialization really is and really does and why the you get a different return value. – Joachim Sauer Jul 06 '21 at 12:14
  • @JoachimSauer Thanks a lot. I understand now how it works and why it is filed. I do not understand why my question was decreased. – Valentyn Hruzytskyi Jul 07 '21 at 11:51

0 Answers0