Skip to content

Classes

Philip Ford edited this page Aug 27, 2017 · 27 revisions

Contents

Syntax

  • Declared just like Java
  • Omitting the access modifier gives the class public visibility.

Properties

  • In Groovy, getters and setters are automatically generated for class members.

Constructors

  • Can be overloaded.
  • Formal parameters do not need types.
  • Can be called either with positional or named parameters1

Default Constructors

if you do not create a constructor (i.e., you use the default constructor), you always can invoke the default constructor with named parameters, which map instance properties to actual parameters:

class PersonWOConstructor {                                  
   String name
   Integer age
}
     
// The default constructor for the class above can be invoked in any of the following ways:
def person4 = new PersonWOConstructor()                      
def person5 = new PersonWOConstructor(name: 'Marie')         
def person6 = new PersonWOConstructor(age: 1)                
def person7 = new PersonWOConstructor(name: 'Marie', age: 2)

This saves you from having to overload the constructor many times to support the different parameter lists.

Declared Constructors

Declared constructors use positional parameters. However, Lists can be coerced into creating instances, when declared constructors exist2:

class PersonConstructor {
    String name
    Integer age

    PersonConstructor(name, age) {          
        this.name = name
        this.age = age
    }
}

def person1 = new PersonConstructor('Marie', 1)  
def person2 = ['Marie', 2] as PersonConstructor  
PersonConstructor person3 = ['Marie', 3]     

Scripts vs. Classes

Groovy supports both scripts and classes.

A class like this:

// Main.groovy
Main.groovy
class Main {                                    
    static void main(String... args) {          
        println 'Groovy world!'                 
    }
}

...can be written instead like this:

println 'Groovy world!'
  • A script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a run method.3
  • If the script is in a file, then the base name of the file is used to determine the name of the generated script class. For example, if the name of the file is Main.groovy, then the script class is going to be Main.
  • Variables in a script do not require a type defination.
     // This is legal, and equivalent to using "int" to declare the variables.
     x = 1
     y = 2
     assert x+y == 3
  • Scripts can have methods/functions.
  • Methods and functions can be mixed with immediate code.

References

Notes

  1. http://groovy-lang.org/objectorientation.html#_constructors

    In Groovy there are two ways to invoke constructors: with positional parameters or named parameters. The former one is like we invoke Java constructors, while the second way allows one to specify the parameter names when invoking the constructor.

  2. http://groovy-lang.org/objectorientation.html#_constructors

    There is [sic] three forms of using a declared constructor. The first one is the normal Java way, with the new keyword. The others rely on coercion of lists into the desired types. In this case, it is possible to coerce with the as keyword and by statically typing the variable.

  3. The script println 'Groovy world!' is equivalent to this:

    import org.codehaus.groovy.runtime.InvokerHelper
    class Main extends Script {                     
        def run() {                                 
            println 'Groovy world!'                 
        }
        static void main(String[] args) {           
            InvokerHelper.runScript(Main, args)     
        }
    }

Clone this wiki locally