Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does final mean in Groovy

Tags:

groovy

final

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

like image 267
Dónal Avatar asked Oct 30 '09 20:10

Dónal


1 Answers

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.

like image 122
Rob Hruska Avatar answered Nov 06 '22 16:11

Rob Hruska