I read this article:
https://www.ibm.com/developerworks/java/library/j-javadev2-8/index.html
The abstract class Model in Listing 2. have static variable datastore.
abstract class Model {
static def datastore = DatastoreServiceFactory.datastoreService
...
The class Race in Listing 3. extends the abstract class Model.
class Race extends Model {
public Race(params){
super(params)
}
}
In Listing 5. and Listing 6.use the author no-static variable datastore (this.datastore) in static method. I suppose, the static method is in class Race.
static def findByName(name){
def query = new Query(Race.class.simpleName)
query.addFilter("name", Query.FilterOperator.EQUAL, name)
def preparedQuery = this.datastore.prepare(query)
if(preparedQuery.countEntities() > 1){
return new Race(preparedQuery.asList(withLimit(1))[0])
}else{
return new Race(preparedQuery.asSingleEntity())
}
}
How is it possible? Thanks for explanation.
Tom
EDIT — you were right, I was on the complete wrong track before. The answer is simple, in groovy, you can use the ‘this’ keyword in static methods.
http://groovy.codehaus.org/Differences+from+Java
When used like that, ‘this’ refers to the class, not an instance. Groovy.