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
$47.50 $59.99
Save 21%
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
$26.98 $35.00
Save 23%
Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)
3 Groovy Programming: An Introduction for Java Developers

Groovy Programming: An Introduction for Java Developers

BUY & SAVE
$55.63
Groovy Programming: An Introduction for Java Developers
4 Making Java Groovy

Making Java Groovy

  • AFFORDABLE PRICES ON QUALITY USED BOOKS IN GOOD CONDITION.
  • ECO-FRIENDLY CHOICE-REDUCE WASTE BY REUSING PRE-LOVED BOOKS.
  • FAST SHIPPING ENSURES YOUR BOOK ARRIVES QUICKLY AND RELIABLY.
BUY & SAVE
$44.78
Making Java Groovy
5 Groovy in Action

Groovy in Action

  • FAST DISPATCH: SAME DAY FOR ORDERS BEFORE NOON!
  • MINT CONDITION GUARANTEE FOR PRISTINE PRODUCT QUALITY.
  • HASSLE-FREE RETURNS: NO QUIBBLES, EASY RETURN PROCESS!
BUY & SAVE
$25.56 $49.99
Save 49%
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 PRICING ON QUALITY USED BOOKS FOR BUDGET-CONSCIOUS READERS.
  • ENVIRONMENTALLY FRIENDLY CHOICE-REDUCE WASTE BY BUYING USED.
  • THOROUGHLY VETTED FOR GOOD CONDITION; ENJOY RELIABLE READING EXPERIENCE.
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
$32.89 $37.99
Save 13%
Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript
8 JSON at Work: Practical Data Integration for the Web

JSON at Work: Practical Data Integration for the Web

BUY & SAVE
$28.66 $49.99
Save 43%
JSON at Work: Practical Data Integration for the Web
9 Reactive Programming with RxJava: Creating Asynchronous, Event-Based Applications

Reactive Programming with RxJava: Creating Asynchronous, Event-Based Applications

BUY & SAVE
$36.85 $44.99
Save 18%
Reactive Programming with RxJava: Creating Asynchronous, Event-Based Applications
10 Groovy: Grundlagen und fortgeschrittene Techniken (German Edition)

Groovy: Grundlagen und fortgeschrittene Techniken (German Edition)

BUY & SAVE
$50.00
Groovy: Grundlagen und fortgeschrittene Techniken (German Edition)
+
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.