Message-ID: <701021099.3425.1394491295033.JavaMail.email@example.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3424_129475930.1394491295033" ------=_Part_3424_129475930.1394491295033 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
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 (in= cluding static imports). Groovy automatically imports the following:<= /p>=20
One difference between Java and Groovy is that by default things ar= e public unless you specify otherwise.=20
Groovy also merges the idea of fields and properties together to make co= de simpler, please refer to the Gr= oovy Beans section for details of how they work.=20
Each class in Groovy is a Java class at the bytecode / JVM level. Any me= thods declared will be available to Java and vice versa. You can specify th= e types of parameters or return types on methods so that they work nicely i= n normal Java code. Also you can implement interfaces or overload Java meth= ods using this approach.=20
If you omit the types of any methods or properties they will default to = java.lang.Object at the bytecode/JVM level.=20
You can also use another class implemented in Groovy. e.g.=20 =20 =20 =20
Make sure the classpath is OK.=20
Groovy supports plain scripts, which do not have a class declaration. Im= ports 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:=20 =20
If you compile the above script to bytecode using groovyc, you get a single class named after the =
name of the script. e.g. if this was saved in Foo.script you'd get a Foo.cl=
You can run this Java code on the command line (assuming yo= u're classpath has groovy.jar and asm.jar).
This will execute the autogenerated main(String args) method in the by= tecode which instantiates the Foo class, which extends the=20
S= cript class and then call its run() method. You may also = use this class directly in Java code, passing in variables to the script.= p>=20=20
There's no need to use a Binding if you don't want to; Foo will have a n= o-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.=20
Unlike classes, variables are not required to be declared (def = is not required) in scripts. Variables referenced in a script are automatic= ally created and put into the Binding.=20
If you just want to write some simple scripts and need some simple funct=
ions you can declare functions without writing a class.
One dif= ference from normal class-based groovy is that the def keyword is = required to define a function outside of a class.
Here's an example of a simple script with a function. Note that if= ever you need things like static or instance variables and so forth then m= aybe its time to actually write a class=20