[Solved] How to call public boolean equals(Object other) [closed]


This is really confusing but you are missing something basic here. equals() is just a method, and you test its return value just like any other method.

In this case, it returns a boolean, which is what the if statement wants, hence there is no need for testing == to some value.

public class MyTest {

    public boolean equals(Object other)
    {        
     if (this==other)
     {
         return true;
     } else {
         return false;
     }
    }

}

Other method:

public static void main(String[] args){
    MyTest test = new MyTest();
    if (test.equals(new MyTest())  // no need for "== true", it's already boolean
       System.out.print("It is valid")
    else
       System.out.print("It is NOT valid")
}

2

solved How to call public boolean equals(Object other) [closed]