---
title: "Program structure in Groovy"
canonical: "https://support.appfire.com/space/JMWE/462062477/Program%20structure%20in%20Groovy"
format: markdown
---
> Macro (aura-html)

This section explains the program structure in Groovy. You can refer to the [official documentation](http://www.groovy-lang.org/structure.html) of the Groovy language for more information.

**On this page:**


## Packages

<span style="color: #000000">A</span><span style="color: #000000"> p</span>ackage<span style="color: #000000"> is a group of similar types of classes, interfaces and sub-packages. It is used to categorize the classes and interfaces so that they can be easily maintained. See </span>[<span style="color: #000000">here</span>](http://www.groovy-lang.org/structure.html#_package_names)<span style="color: #000000"> to learn more about packages.</span>

## <span style="color: #404040">Imports</span>

<span style="color: #222222">In order to refer to any class, you need a qualified reference to its package. Groovy follows Java’s notion of allowing</span><span style="color: #222222"> </span>`import `<span style="color: #222222">statement to resolve class references. </span><span style="color: #000000">The import allows accessing classes of a package without package qualification. See </span>[<span style="color: #000000">here</span>](http://www.groovy-lang.org/structure.html#_imports)<span style="color: #000000"> to learn more about imports.</span>

<span style="color: #000000">For example, the </span>[**MarkupBuilder**](http://groovy-lang.org/processing-xml.html#_markupbuilder) class (for creating XML with Groovy)<span style="color: #343437"> </span><span style="color: #222222">is inside the </span>`groovy.xml`<span style="color: #222222"> package.</span><span style="color: #343437"> S</span><span style="color: #343437">o, in order to use this class, you need to</span><span style="color: #343437"> </span>`import`<span style="color: #343437"> </span><span style="color: #343437">it as shown:</span>

```groovy
// importing the class MarkupBuilder
import groovy.xml.MarkupBuilder
```

### <span style="color: #222222">**Default imports**</span>

<span style="color: #222222">Groovy automatically imports the packages </span>`groovy.lang.*, groovy.util.* , java.lang.* , java.util.* , java.net.* , and java.io.*`<span style="color: #222222"> as well as the classes </span>`java.math.BigInteger`<span style="color: #222222"> and </span>`BigDecimal`<span style="color: #222222">, since these are most commonly used.</span>

## Objects

In Groovy everything is an object that has methods and properties. 

**Creating an object**

In continuation to the above example, to create an XML you need to first create an object from the imported class as shown below:

```groovy
// using the imported class to create an object
import groovy.xml.MarkupBuilder
def xml = new MarkupBuilder()
```

## <span style="color: #222222">Methods</span>

<span style="color: #222222">A m</span><span style="color: #000000">ethod </span><span style="color: #222222">is a set of code which is referred to by name and can be invoked </span><span style="color: #404040">with parentheses </span>`()`<span style="color: #404040"> that may contain arguments. It can be invoked </span><span style="color: #222222">at any point in a program simply by utilizing its</span><span style="color: #222222"> name. A method is </span><span style="color: #000000">where you put the operations on data (variables) in your Groovy code. </span>

<span style="color: #222222">**Accessing a method**</span>

<span style="color: #222222">In continuation to the above example, you can access a method using the "." dot syntax as shown in the below example:</span>

```groovy
import groovy.xml.MarkupBuilder
def xml = new MarkupBuilder()
//Accessing a method of the class
xml.getMkp()
```

### **Direct access operator**

<span style="color: #404040">You can use the direct access operator to </span><span style="color: #222222">call the property directly removing the parenthesis () and the getter.</span>

```groovy
import groovy.xml.MarkupBuilder
def xml = new MarkupBuilder()
//Accessing a method of the class
xml.mkp
```

## Variables

<span style="color: #000000">Variables are typically used to store information which your Groovy script needs to do its job on. See </span>[<span style="color: #000000">here</span>](http://www.groovy-lang.org/structure.html#_variables)<span style="color: #000000"> for more information on Variables and their types. </span>

**<span style="color: #222222">Variable declaration</span>**

<span style="color: #222222">You can declare a variable either by specifying its type, or using </span>*<span style="color: #222222">def</span>*<span style="color: #222222">. </span><span style="color: #222222">When using </span>`def`<span style="color: #222222">, the variable will be able to refer to objects of any type</span><span style="color: #404040">.</span>

```groovy
int x = 1
int y = 2
x + y == 3

x = 1
y = 2
x + y == 3
```