Multiple Assignment Add comment to Wiki View in Wiki Edit Wiki page Printable Version

Multiple Assignment

Groovy supports multiple assignment, i.e. where multiple variables can be assigned at once, e.g.:

You can provide types as part of the declaration if you wish, e.g.:

As well as used when declaring variables (as above) it also applies to existing variables, e.g.:

The syntax works for arrays as well as lists, as well as methods that return either of these, e.g.:

Overflow and Underflow

If the left hand side has too many variables, excess ones are filled with null's, e.g.:

If the right hand side has too many variables, the extra ones are ignored, e.g.:


  • currently only simple variables may be the target of multiple assignment expressions, e.g. if you have a person class with firstname and lastname fields, you can't currently do this:
  • currently multiple assignment cannot be used for declaring multiple class fields and initializing them as:



Results of your search request can come from various sources: the Groovy website itself, the JIRA issues, the API documentation, as well as a few other interesting Groovy-related blogs.

  By  -  pages  -  views  - last modified