The non-static variable "this" in the static method

I read this article: https://www.ibm.com/developerworks/java/library/j-javadev2-8/index.html

The abstract class model in Listing 2. has a datastore static variable.

abstract class Model { static def datastore = DatastoreServiceFactory.datastoreService ... 

The Race class in Listing 3. extends the abstract class model.

 class Race extends Model { public Race(params){ super(params) } } 

In Listing 5. and Listing 6. Use a static storage file with no static value ( this.datastore ) in the static method. I suppose the static method is in the Race class.

 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 this possible? Thanks for the explanation.
Tom

+4
source share
2 answers

EDIT - you were right, I used to be on the wrong track. The answer is simple, in groovy you can use the 'this' keyword in static methods.

http://groovy.codehaus.org/Differences+from+Java

When used in this way, 'this' refers to a class, not an instance. Groovy.

+3
source

In - statically compiled - Java, static code that invokes instance code will not compile. This is because of the high risk that static code is not called by the instance, but called by type, directly - which will lead to runtime errors.

While it is theoretically possible to compile Groovy code statically, the default is to compile it dynamically. “Dynamically” means that, in part, there are no direct links between code artifacts (such as methods, variables, constructors, classes, etc.) in the bytecode.

Consider the following Groovy class:

 class StaticClass { static def someString = "someString" static def staticMethod() { this.someString } static def main(args) { StaticClass.staticMethod() new StaticClass().staticMethod() } } 

Using the Groovy compiler, groovyc , it is compiled into code that provides dynamic execution at runtime. The following code represents the above main(..) method after compilation:

 public static void main(String[] args) { CallSite[] arrayOfCallSite = $getCallSiteArray(); arrayOfCallSite[0].call($get$$class$StaticClass()); arrayOfCallSite[1].call(arrayOfCallSite[2].callConstructor( $get$$class$StaticClass())); return; } 

What really happens when this code is called is hard to say, because at runtime there is a very complex and nested flow of decisions and a workflow that depends on several factors.

As in this simple example, there are no special cases (metaclass, expandometaclass, closures, interceptors, etc.), it is likely that, after all, the implementation of GroovyObject.invokeMethod(String, Object) .

Throughout the Groovy documentation (including the User Guide and Wiki, but until the missing JavaDocs and missing source files in the source distribution), the mechanics of this “dynamic metaprogram runtime” are hidden from the user - perhaps because of this, it’s a huge complication and because the user doesn’t You need to know about this in detail. This is quite interesting for the main Groovy developers.

So, in the end, it's hard to say why this works when the object instance is not called. Another question: should it work ...

+3
source

All Articles