concept constructor in category groovy

appears as: constructor, constructors, constructor, constructors, The constructors
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

A Groovy bean can be marked as immutable, which means that the class becomes final, all its fields become final, and you cannot change its state after construction. Listing 2.5 declares an immutable FixedBean class, calls the constructor in two different ways, and asserts that you have a standard implementation of equals() that supports comparison by content. With the help of a little try-catch, you assert that changing the state isn’t allowed.

Listing 7.9. Calling constructors with positional parameters
Listing 7.10. Calling constructors with named parameters
class SimpleVendor { String name, product } new SimpleVendor() new SimpleVendor(name: 'Canoo') new SimpleVendor(product: 'ULC') new SimpleVendor(name: 'Canoo', product: 'ULC') def vendor = new SimpleVendor(name: 'Canoo') assert 'Canoo' == vendor.name

8.5.2. Replacing constructors with factory methods

New objects are usually constructed by using the new keyword and a constructor as in new Integer(42). Many question this language design and you often hear the advice to favor factory methods over direct constructor calls.