How Groovy’s def Keyword Enables Dynamic Typing and Reduces Boilerplate

This article explains Groovy’s def keyword as a mutable, untyped variable, shows how it differs from Java’s var, demonstrates dynamic reassignment, and provides a practical JSON‑to‑object conversion example that simplifies code while leveraging IntelliJ’s type inference.

FunTester
FunTester
FunTester
How Groovy’s def Keyword Enables Dynamic Typing and Reduces Boilerplate

In earlier posts the author introduced the basic usage of Groovy’s def keyword, describing it as an untyped variable similar to the “no‑type” concept in some documentation.

The simplest form is:

def a = 1
def b = "FunTester"

While Java’s recent var can replace explicit type declarations in some cases, Groovy’s def remains superior because it not only replaces explicit types but also allows the variable to hold objects of different types over its lifetime.

For example, the same variable can be reassigned with a different type:

def a = 1
a = "FunTester"

This reassignment would cause a compile‑time error in Java, but Groovy permits it, and IntelliJ IDEA can infer the variable’s type during editing.

The author’s key insight is that def creates a mutable‑type variable whose actual type can change through assignment.

One practical use is reducing code when converting a batch of JSON strings into a Map or JSON object where the values themselves need to become typed objects.

Consider the following Demo class:

private static class Demo {
    Demo(int age, String name) {
        this.age = age
        this.name = name
    }
    int age
    String name
    @Override
    public String toString() {
        return "Demo{age=" + age + ", name='" + name + "'}";
    }
}

And a JSON string containing several entries:

{"a":{"age":1,"name":"FunTester123"},"b":{"age":2,"name":"FunTester123"},"c":{"age":3,"name":"FunTester123"}}

The conversion code is:

def config = JSON.parseObject(str).each { it.value = new Demo(it.value.age, it.value.name) }
output(config.a)

The console output shows that each map entry’s value has been transformed into a Demo instance:

22:48:18.367 main Demo{age=1, name='FunTester123'}

IntelliJ initially infers config as Map<String, Object>, but at runtime it becomes Map<String, Demo>. For better IDE assistance, you can append as Map<String, Demo> to the first line, forcing the static type.

Additionally, def can be used as a method’s return type, allowing a single method to return objects of different types.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentGroovyjson-parsingIntelliJdynamic typingdef
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.