Assuming the following code snipped from a method within a groovy class, which makes use of a callback:
Service.groovy:
class Service {
final Logger logger
}
QueryService.groovy:
class QueryService extends Service {
def someMethod() {
Logger l = logger
future.register(new SingleResultCallback<ArrayList<Document>>() {
@Override
void onResult(ArrayList<Document> result, MongoException e) {
l.info('result: ' + result) // works
logger.info('result: ' + result) // fails
}
})
}
}
There are two things I am wondering about:
- Why is it possible to assign the logger variable to a local variable and access it from within the callback? Why can't I access a final instance variable? Does the callback method lose the reference to the class, but still have the method scope from which it is called from? What is going on internally?
- Is there a way to still have access to an instance variable, without having to assign it every time before making use of a callback?
In case anybody is wondering as of the java reference within groovy. This is from using the upcoming mongodb java driver.