Can I have only getters and not a setter?

I'm new to groovy, and I found that by creating a public post, groovy provides default getters and seters. Is there a default way to use only getters, but not setters? The reason for this is that I have a Builder, and I do not want to provide access to the fields of the object for modification.

+4
source share
1 answer

You can create fields finaland add a transformation Canonicalto get an automatically created c'tor. Or even easier to use Immutabletransform:

@groovy.transform.Immutable
class A {
    String x
}

def a = new A("x")
assert a.x == "x"
// a.x = "will fail"
// a.setX("will fail")

builder transforms, .

+5

All Articles