Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added import info and note about def not required in scripts, fixed links

Classes are defined in Groovy similarly to Java. Methods can be class (static) or instance based and can be public, protected, private and support all the usual Java modifiers like synchronized.  Package and class imports use the Java syntax (including static imports).  Groovy automatically imports the following:One difference with Groovy

  • java.lang
  • java.math
  • java.util
  • groovy.lang
  • groovy.util

One difference between Java and Groovy is that by default things are public unless you specify otherwise.


Groovy supports plain scripts, which do not have a class declaration. Imports are supported at the front of a script in the same way that they can be a the front of a class. Here's the hello world script:

Code Block
println "Nice cheese Gromit!"


This will execute the autogenerated main(String[] args) method in the bytecode which instantiates the Foo class, which extends thethe 

class and then call its run() method. You may also use this class directly in Java code, passing in variables to the script.


There's no need to use a Binding if you don't want to; Foo will have a no-argument constructor as well. Though using a Binding you can easily pass in variables. After the end of the script any variables created will be in the Binding for you to access in Java.

Unlike classes, variables are not required to be declared (def is not required) in scripts. Variables referenced in a script are automatically created and put into the Binding.

Scripts and functions

If you just want to write some simple scripts and need some simple functions you can declare functions without writing a class.
One difference from normal class-based groovy is that the def keyword is required to define a function outside of a class.