Recommended Free Tools
Groovy’s tap method lets you configure an object inside a closure and then returns that same object. It is useful for builder-style setup because you can assign properties and call methods without repeating the variable name, while keeping the configured instance available for the next step in an expression.
What Groovy’s tap method does
tap calls a closure for its receiver and always returns the receiver. Apache Groovy’s API describes it as calling the closure for the object reference self and returning self. Inside the closure, you can configure the object with property assignments and method calls.
For example, this creates a Sample, sets its properties, adds labels, and assigns the configured instance to sample:
class Sample {
String username, email
List<String> labels = []
void addLabel(value) { labels << value }
}
def sample = new Sample().tap {
username = 'mrhaki'
email = 'email@host.com'
addLabel 'Groovy'
addLabel 'Gradle'
}
The closure is a convenient place for repeated setup. Because tap returns the Sample instance rather than the closure’s final result, the expression still evaluates to the configured object. The Apache Groovy style guide presents tap as the builder-style option when the incoming object should be returned: Groovy style guide.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11How tap differs from with
tap is equivalent to with(true). The difference that matters most is the value returned after the closure runs: ordinary with returns the closure’s result, while with(true) and tap return the original receiver. Groovy closures normally return their last expression, so a final method call or value can determine what ordinary with returns.
| Form | Returned value | Best fit |
|---|---|---|
with { ... } or with(false) { ... } |
The closure’s result | Use when the closure computes a different value from the receiver. |
with(true) { ... } |
The original receiver | Use when you want with’s closure scope and need the configured object back. |
tap { ... } |
The original receiver | Use for builder-style configuration when the receiver should continue through the expression. |
| Explicit property and method calls | The value of the surrounding expression; individual statements do not implicitly return a configured object | Use when direct receiver references make the setup clearer or compatibility with an older runtime matters. |
For example, if the last expression in a with closure is a value or a method result, that result—not the configured receiver—becomes the value of the whole with expression. tap avoids that accidental change when your intent is to keep the object. The Apache Groovy API documents the tap/with return behavior: Object.tap API reference.
When to use tap for object creation
Use tap when you create an object, perform several setup operations on it, and want the configured object to remain the result. This is especially useful in a longer expression or when assigning a newly configured instance directly to a variable.
- Choose
tapfor property assignments and setup calls when the configured receiver should be returned. - Choose
withwhen the closure is meant to calculate or return a value that differs from the receiver. - Choose explicit calls when repeating the receiver improves clarity, or when your target Groovy runtime predates
tap.
Groovy’s style guide describes tap as a builder-style form for repeated operations on one bean. The historical Apache issue behind the method explains the motivation: avoid writing an explicit return delegate just to retain the configured object.
Rank #3
Groovy version compatibility
tap was added to all objects in Groovy 2.5.0. The Apache Groovy API reference also marks the tap/boolean-with behavior as available since 2.5.0. If your application targets an older Groovy runtime, confirm that version’s support before using tap; explicit configuration calls remain an alternative.
Quick Recap
Best Value
Rank #4
- Used Book in Good Condition
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

