How Does @Immutable Work In Groovy?

7 minutes read

The @Immutable annotation in Groovy is used to create immutable classes, which means objects of these classes cannot be modified after they are initialized. When the @Immutable annotation is applied on a class, Groovy automatically generates a set of getters and constructors, as well as overrides equals(), hashCode(), and toString() methods.


Here's how @Immutable works in Groovy:

  1. Annotating a Class: To make a class immutable, you simply need to annotate it with @Immutable as follows: import groovy.transform.Immutable @Immutable class MyClass { String name int age }
  2. Generated Methods: The @Immutable annotation generates the following methods automatically: Getters: Get methods are created for each property defined in the class. In this example, getName() and getAge() are generated. Constructors: A no-argument constructor and a constructor that takes all properties as arguments are generated. equals(), hashCode(), and toString(): These methods are overridden based on the properties of the class.
  3. No Setter Methods: Once a class is annotated with @Immutable, no setter methods will be generated for its properties. This ensures that class instances remain read-only.
  4. Final Fields: Properties in an @Immutable class are treated as final fields. They can only be assigned values during object construction.
  5. Immutable Objects: Once an object of an @Immutable class is created, its properties cannot be modified. Any attempt to change them will result in an exception.


Using the @Immutable annotation simplifies the implementation of immutable classes in Groovy by generating the necessary boilerplate code. It guarantees that objects of these classes cannot be accidentally modified, providing thread safety and effective data encapsulation.

Best Groovy Books to Read in 2024

1
Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

Rating is 5 out of 5

Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

2
Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

Rating is 4.9 out of 5

Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

3
Mastering GROOVY: A Comprehensive Guide To Learn Groovy Programming

Rating is 4.8 out of 5

Mastering GROOVY: A Comprehensive Guide To Learn Groovy Programming

4
Groovy Programming: An Introduction for Java Developers

Rating is 4.7 out of 5

Groovy Programming: An Introduction for Java Developers

5
Making Java Groovy

Rating is 4.6 out of 5

Making Java Groovy

6
Groovy in Action: Covers Groovy 2.4

Rating is 4.5 out of 5

Groovy in Action: Covers Groovy 2.4


Can a class with a no-arg constructor be annotated with '@immutable'?

Yes, a class with a no-arg constructor can be annotated with '@immutable'. The '@immutable' annotation indicates that the class is immutable, meaning its state cannot be changed after it is created. Even if the class has a no-arg constructor, it can still be considered immutable if it meets the criteria for immutability.


Can '@immutable' classes have mutable properties?

No, '@immutable' classes cannot have mutable properties. The purpose of using the '@immutable' decorator is to indicate that the class should not be modified after its creation. This means that all properties of an '@immutable' class should be immutable as well.


How does the '@immutable' annotation affect the behavior of a Groovy class?

The @immutable annotation in Groovy affects the behavior of a class by making it immutable, which means that once an instance of the class is created, its state cannot be modified. Here are some ways in which the @immutable annotation affects a Groovy class:

  1. Constructor: The annotation adds a constructor to the class that takes all properties as arguments. This constructor ensures that all properties are initialized when an instance of the class is created.
  2. Getters: The annotation adds getter methods for all properties, allowing access to their values but not allowing modification.
  3. Setters: The annotation does not add setters for the properties, as the class is immutable and the properties cannot be modified.
  4. hashCode and equals: The annotation adds hashCode() and equals() methods that are based on the class properties. These methods use the properties' values to calculate the hash code and compare equality, ensuring that two instances with the same property values are considered equal.
  5. toString: The annotation adds a toString() method that includes the class name and all property values.


By making a class immutable, the @immutable annotation provides guarantees about the class's state and behavior, making it easier to reason about and use in multi-threaded or concurrent environments. It also promotes immutability, which can help in designing more reliable and predictable code.

Twitter LinkedIn Telegram Whatsapp

Related Posts:

In Groovy, an immutable class, by definition, means that the state of the object cannot be modified after its creation. However, Groovy introduces some flexibility with regards to immutability.By default, Groovy classes are mutable, which means that the proper...
In Kotlin, you can initialize a big immutable map using the mapOf() function or the to notation. Here's how you can achieve it:Using the mapOf() function: Initialize a big immutable map by using the mapOf() function, providing key-value pairs in the form o...
To call a Groovy script from a Jenkins file, you can follow these steps:First, make sure you have the necessary plugins installed on your Jenkins server to support Groovy scripting. In your Jenkins pipeline or job, create a new stage or step where you want to ...