When I execute the code below in the groovy console, I get groovy.lang.ReadOnlyPropertyException . As expected, since the x property cannot be changed (since ClassA is immutable).
import groovy.transform.Immutable @Immutable class ClassA { int x } def a = new ClassA(x: 5); ax = 1
But if I change the access modifier to private for the variable x , then I can do this in the groovy console:
import groovy.transform.Immutable @Immutable class ClassA { private int x } def a = new ClassA(x: 5); ax = 1 println(ax)
Why is this? Why does the added private access modifier make ClassA mutable?
source share