Wednesday, January 20, 2016

Java Puzzler

I thought I already knew about Java's static and dynamic binding rules, but after trying this code snippet, I had to learn a few more things. Looks like those binding rules don't actually cover arguments and parameters.


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);
    }
}

Source

1 comment:

  1. Nice work done by the blogger by illustrating example how to use dynamic binding in Java programming. This is short but great work. I am very glad to find such a good work.

    ReplyDelete