Groovy closure does not work with static end field from superclass

class Parent { final static String newLine = "*" } class Child extends Parent{ List body = [3, 4, 5] String toString() { def str = new StringBuilder() body.each { str.append(it + newLine) } str } } def c = new Child() println c 

The above example is a trivial example illustrating the problem. It cannot be compiled using the Groovy plugin on Eclipse . Remove either final or static in the superclass field. However, I have no idea why this is so.

http://groovy.codehaus.org/Groovy+Beans This link mentions the rules for properties and fields used in Groovy. I believe that the one that was applied should be the last, i.e. Meta class. Unfortunately, I still could not understand the behavior.

The behavior is played sequentially in all versions of Groovy. Perhaps someone might report the error to the Groovy team. I have never done this before. It would be more effective if someone experienced this.

+4
source share
2 answers

This is most likely http://jira.codehaus.org/browse/GROOVY-5776 , which is harder to fix than it looks

+2
source

As blackdrag has already pointed out: this is a mistake. But another workaround is to add the protected keyword:

 protected final static String newLine = "*" 
+1
source

All Articles