HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GroovyVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Groovy›Variables

Variables

Variable definition with def, either without specifying a type or by providing a type.

Simple enough for a 5-year-old to understand

variable with def

you don't specify the type with def, it is inferred from the value. You can also explicitly write the type: int, double, String.

Example code
1
def name = "Alex"
Type is inferred from the value.
VariableType is inferred from the value.
2
int age = 36
Explicit type: int.
VariableExplicit type: int.
3
println name
Alex
Output / InputAlex
4
println age
36
Output / Input36
VariableOutput / Input
💡 Hint: def = variable whose type is inferred from the value.

About this topic

There are two ways to define variables in Groovy: dynamic typing with def, or static declaration by explicitly writing the type. Both are valid and can be mixed in the same file.

There is an important and lesser-known rule in script files: variables defined with def are in the script's local scope and are not visible to methods in the same file. Variables defined without def enter the binding and are accessible from anywhere.

Not knowing this distinction leads to frequent and hard-to-understand errors in Gradle and Jenkins scripts. The rule is simple: define values that will be accessed from methods without def, or pass them all as method parameters.

What you will learn

  • Dynamic definition with def
  • Explicit type declaration
  • The difference between script scope and binding
  • Defining constants
  • Naming conventions

Who is this for?

  • Those who write Gradle and Jenkins scripts
  • Those who cannot access a variable from inside a method
  • Those new to dynamic typing

When to use?

Using def in short scripts is quick; declaring types in long and shared code catches errors early.

For values defined at the script level and used by methods, it is necessary to use a binding variable.

Common mistakes

Wrong

def mesaj = "Hello"

def yazdir() {
  println mesaj
}
yazdir()

A script variable defined with def is not visible from the method; this code gives a "No such property: message" error. This is the most confusing behavior of Groovy scripts because a variable that is right there in the same file cannot be accessed.

Correct

mesaj = "Hello"

def yazdir() {
  println mesaj
}
yazdir()

Wrong

int sayi = "hello"
println sayi

Even though Groovy is dynamic, an assignment that doesn't match the declared type produces a GroovyCastException at runtime. When you declare a type, the language takes it seriously; it doesn't convert it silently. This is the fundamental difference between def and a type declaration.

Correct

String sayi = "hello"
println sayi

Wrong

final SABIT = 10
SABIT = 20

A variable marked with final cannot be reassigned, resulting in an error. Defining constants at the script level prevents configuration values from being accidentally changed; this discipline comes in handy in large build scripts.

Correct

final SABIT = 10
def yeniDeger = 20

Frequently asked questions

Which one should I choose between def and type declaration?

def is sufficient for short scripts and allows for quick typing. Declaring types in code that multiple people will touch or that is long-lasting allows you to catch errors before runtime.

What exactly is a binding variable?

It is a space consisting of name and value pairs maintained while the script is running. Every variable you assign without writing def goes here and is accessible from anywhere, including methods. In Gradle, project properties are also exposed via this mechanism.

How do I find out the type of a variable?

The property that queries the class information of the object is used. When you get unexpected behavior in dynamic code, this is the first place to look; most of the time it turns out the value is not the type you thought it was.

Summary

def gives a dynamic type, while an explicit declaration gives a static type.

A variable defined with def in the script is not visible from methods.

A variable defined without def enters the binding.

An assignment that does not match the declared type produces an error.

final prevents reassignment.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code