10

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 Answer 1

2

It looks like this might be a Groovy bug:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.