Software Development

Immutable Objects Using Annotation In Groovy

In Groovy, we can create immutable objects using the @Immutable annotation.

Immutable Objects

The annotation makes an immutable object.

Immutable Annotation

To instantiate and initialize the object’s properties, use the constructor with named parameters.

Testing with Immutable Objects

Let’s test out the class by instantiating it with values and modifying one of the properties.

Line 11 throws a ReadOnlyPropertyException.

References

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like