Return to Snippet

Revision: 11507
at February 7, 2009 17:09 by narkisr


Initial Code
class Class1 {
  def closure = {
    println this.class.name
    println delegate.class.name
    def nestedClos = {
      println owner.class.name
    }
    nestedClos()
  }
}

def clos = new Class1().closure
clos.delegate = this
clos()
/*prints:
 Class1
 Script1
 Class1$_closure1  */

Initial URL


Initial Description
This, owner, and delegate:
this : as in Java, this refers to the instance of the enclosing class where a Closure is defined
owner : the enclosing object (this or a surrounding closure)
delegate : by default the same as owner, but changeable.
(http://groovy.codehaus.org/Closures).

Initial Title
Groovy delegate, owner, this references.

Initial Tags
groovy

Initial Language
Groovy