groovy Closures Create closures, assign to properties and call

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Let's create a map and a closure to print hello

def exMap = [:]

def exClosure = {
    println "Hello"
}

Assign closure to a property in map

exMap.closureProp = exClosure

Calling closure

exMap.closureProp.call()

Output

Hello

Another Example - Lets create a class with basic property and assign same closure to object of it

class Employee {
    def prop
}

def employee = new Employee()

employee.prop = exClosure

Call closure through that property

employee.prop.call()

Output

Hello


Got any groovy Question?