Search code examples
groovythisowner

Groovy -- What is the difference between this and owner


according to this this and owner seem to have the same meaning. Reading further I find that owner can refer to the enclosing class or closure. Is this the only difference? If so, why reserve an entire word just for that?


Solution

  • this refers to the enclosing class instance. owner refers to the directly enclosing object, which may or may not be the enclosing class instance. For example:

    def x = { def y = { println this; println owner }; y() }
    x()
    

    Here this refers to the instance of the script class, and owner refers to x.