Skip to main content
freelanceshack.com

Back to all posts

How Does @Immutable Work In Groovy?

Published on
4 min read
How Does @Immutable Work In Groovy? image

Best Groovy Programming Books to Buy in October 2025

1 Groovy in Action: Covers Groovy 2.4

Groovy in Action: Covers Groovy 2.4

BUY & SAVE
$28.80 $59.99
Save 52%
Groovy in Action: Covers Groovy 2.4
2 Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

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

BUY & SAVE
$30.94 $35.00
Save 12%
Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)
3 Making Java Groovy

Making Java Groovy

  • QUALITY ASSURANCE: EACH BOOK CHECKED FOR GOOD CONDITION AND READABILITY.
  • ECO-FRIENDLY CHOICE: SUPPORT SUSTAINABILITY BY BUYING USED BOOKS.
  • COST SAVINGS: ENJOY GREAT BOOKS AT SIGNIFICANTLY LOWER PRICES!
BUY & SAVE
$40.14 $44.99
Save 11%
Making Java Groovy
4 Groovy Programming: An Introduction for Java Developers

Groovy Programming: An Introduction for Java Developers

BUY & SAVE
$58.56 $65.95
Save 11%
Groovy Programming: An Introduction for Java Developers
5 Groovy in Action

Groovy in Action

  • FAST DISPATCH: SAME-DAY SHIPPING ON ORDERS BY 12 PM!
  • MINT CONDITION: EVERY PRODUCT ARRIVES IN PERFECT SHAPE.
  • HASSLE-FREE RETURNS: GUARANTEED SATISFACTION WITH NO QUIBBLES!
BUY & SAVE
$24.14 $49.99
Save 52%
Groovy in Action
6 Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

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

  • AFFORDABLE PRICES VS. NEW BOOKS, SAVINGS FOR BUDGET-CONSCIOUS READERS.
  • ECO-FRIENDLY OPTION: SUPPORT SUSTAINABILITY THROUGH REUSED BOOKS.
  • QUALITY ASSURANCE: THOROUGH INSPECTION ENSURES GOOD CONDITION ALWAYS.
BUY & SAVE
$34.95
Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)
7 Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript

Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript

BUY & SAVE
$30.95 $37.99
Save 19%
Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript
8 The C Programming Language

The C Programming Language

BUY & SAVE
$107.69
The C Programming Language
9 Spock: Up and Running: Writing Expressive Tests in Java and Groovy

Spock: Up and Running: Writing Expressive Tests in Java and Groovy

BUY & SAVE
$46.52 $59.99
Save 22%
Spock: Up and Running: Writing Expressive Tests in Java and Groovy
10 Groovy Oceans Of Possibilities Octopus Summer Reading Book T-Shirt

Groovy Oceans Of Possibilities Octopus Summer Reading Book T-Shirt

  • CELEBRATE SUMMER READING WITH OUR FUN, OCEAN-THEMED BOOK SHIRT!

  • PERFECT GIFT FOR TEACHERS AND BOOK LOVERS AT EVENTS AND FAIRS!

  • LIGHTWEIGHT, COMFY FIT IDEAL FOR LIBRARY EVENTS AND SUMMER FUN!

BUY & SAVE
$17.98
Groovy Oceans Of Possibilities Octopus Summer Reading Book T-Shirt
+
ONE MORE?

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.

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.