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

You Might Also Like