Johan Parent | 26 Dec 17:05
Picon
Favicon

[groovy-user] [1.6.6] static final field groovy != java

Hi all,

I'm a groovy newbie (glaforge's devoxx talk convinced me to give it a try). Here is the thing the surprised me in that it differs from the Java behaviour I had expected. (assert me.java != newby ;)

class A {
    static final A NONE = new A()
}

class B extends A {
    static final B NONE = new B();
}

assert A.NONE instanceof A
assert B.NONE instanceof B  // This will give an assertion error

Apparently the evaluation of B.NONE returns A.NONE  I observed this with groovyConsole and within a "gmaven project" (if you see what I mean) in IntelliJ (1.6.6). This is not what I had expected from something that should be Java compatible. I've put the Java equivalent code below.

Can someone explain why that is? Is this a bug? I have not seen this behaviour with static final read-only properties (which makes more sense to me).  But then again I may just be confused and in need of some enlightened advice.

Groovy is great; thanks for the great work!

Best regards,

Johan


------ java equivalent code of snippet above --------

public class Simple {
    public static void main(String[] args) {
        assert A.NONE instanceof A;
        assert B.NONE instanceof B;
    }
}

class A {
    static final A NONE = new A();
}

class B extends A {
    static final B NONE = new B();
}


Gmane