what does final mean in Groovy

孤者浪人 提交于 2019-12-08 17:13:44

问题


If you run the following code in the Groovy console it prints "8"

class F {

  private final Integer val = 2

  def set(v) {val = v}

  def print() {println val}
}

def f = new F()
f.set(8)
f.print()

In Java this code wouldn't compile because you can't assign a final reference after the constructor has run. I know that for properties, final indicates that the property can't be changed outside the class, but what does it mean to mark a private field final?

Thanks, Don


回答1:


It looks like this might be a Groovy bug:

  • https://issues.apache.org/jira/browse/GROOVY-1628
  • https://issues.apache.org/jira/browse/GROOVY-2752

I wouldn't think that val should be assignable after initialization.



来源:https://stackoverflow.com/questions/1652133/what-does-final-mean-in-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!