groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Дионисьев Павел <xseagu...@gmail.com>
Subject Re: `this` in static context
Date Mon, 06 Nov 2017 17:54:00 GMT
No, I’m not talking about closures. Normal static methods and static initialisers.
Honestly, I remember using it years ago. Not recently though.

It’s a small but handy thing to have when you are defining stuff like loggers manually.


Imagine code like this: // AFAIK it should work now.

```
class LongClassName { 
  private static Logger log = Logger.getLogger(this.name) // instead of duplicating class
name here. Logger.getLogger(LongClassName.class.getName())
}
``` 

It simplifies manual class renaming, and eliminates chances of error.

I was sure it should work, but seems not in IDEA. So, I’m guessing, is it IDEA bug, or intended
behaviour.

Cheers,
Pavel

> On 6 Nov 2017, at 18:45, <eric.milles@thomsonreuters.com> <eric.milles@thomsonreuters.com>
wrote:
> 
> For sure , "this" and "super" are supported within a closure defined in a static context.
 Are you saying they also work directly in a static method?  Given "class A { static main(args}
{ this } }", this should be of type "Class<A>" if it is indeed supported.  It follows
that it would work, since you can call directly methods on Class<A> from that scope,
like getCanonicalName().


Mime
View raw message