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?
Asked
Active
Viewed 707 times
1

srage
- 990
- 1
- 9
- 27

James Parsons
- 6,097
- 12
- 68
- 108
-
Here's a nice article on the topic: http://java.dzone.com/articles/groovy-closures-owner-delegate – Opal May 30 '14 at 07:50
1 Answers
2
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
.

Peter Niederwieser
- 121,412
- 21
- 324
- 259
-
Yep. It's also worthwhile mentioning that `delegate` is slightly different from `owner`. It can be programmatically changed. – Andrew Eisenberg Jun 02 '14 at 04:33