Java code related to equals method

I am practicing for an exam, and found a sample problem that I don't understand.

For the following code, find what the output is:

public class Test {

    private static int count = 0;

    public boolean equals(Test testje) {
        System.out.println("count = " + count);
        return false;
    }

    public static void main(String [] args) {
        Object t1 = new Test();
        Object t2 = new Test();
        Test t3 = new Test();
        Object o1 = new Object();

        ++count; t1.equals(t2);
        ++count; t1.equals(t3);
        ++count; t3.equals(o1);
        ++count; t3.equals(t3);
        ++count; t3.equals(t2);
    }
}

The output of this code is count = 4, but I don't understand why. Can anyone help me?


The first thing you should note is that public boolean equals(Test testje) doesn't override Object's equals, since the argument is Test instead of Object, so the signatures don't match.

Therefore the main method calls equals(Test testje) exactly once - when executing t3.equals(t3); - since that's the only case in which both the static type of the instance equals is executed for and the type of the argument are the Test class.

t3.equals(t3); is the 4th equals statement (which comes after 4 increments of the static count variable), so 4 is printed.

All the other equals statements execute Object's equals, and therefore print nothing.

A more detailed explanation :

t1.equals() calls Object's equals regardless of the type of the argument, since the static (compile time) type of t1 is Object, and the Test class doesn't override that method. The Object class doesn't have an equals method with a single Test argument, so equals(Test testje) can't be called, regardless of the dynamic (runtime type) of t1.

t3.equals() can execute either Object's equals or Test's equals, since the compile time type of t3 is Test, and the Test class has two equals methods (one inherited from the Object class and the other defined in the Test class).

The method being chosen depends on the compile time type of the argument : 1. When the argument is Object (as in t3.equals(o1); or t3.equals(t2);), Object's equals is called and nothing is printed. 2. When the argument is Test, as in t3.equals(t3);, both versions of equals match that argument, but due to the rules of method overloading, the method with the most specific argument - equals(Test testje) - is chosen and the count variable is printed.


The equals method in Test takes an instance of Test.

All the previous attempts have been made with an instance of Object, which take the inherrited method from the Object class:

public boolean equals(Object o){
  return this == o;
}

Since there is no print in there, it won't print any value.

Your ++count; will increment the value of count, so the moment when you actually call your

public boolean equals(Test testje){...

method, that does print that value, the value of count is 4.