开发者

What problems or benefits would be associated with a "static this" keyword?

What I am thinking of is something like the "this" keyword but at a class level i.e.

class Example {
    public static void main(String[] args) {
        assert Example.class == thisclass.class
    }
}

where "thisclass" is a keyword that returns the current class, just like "this" ret开发者_开发百科urns the current instance.

I can see some places where this would be useful (e.g. stopping cut&paste errors in the common logging idiom) but I can also see some potential problems (how to handle subclasses etc).

I assume that there were good reasons not to include this concept in Java but what are they? Additionally, would it be possible to do this sort of thing using Groovy meta programming?


groovy has exactly the 'this' semantics you are talking about already. In a static context, 'this' refers to the class on which the method resides.

check out http://groovy.codehaus.org/Differences+from+Java and search for 'this keyword'.

Java didn't follow suit on this, obviously, probably because in the end its just a small piece of flavor that is 'groovy'. Certainly not necessary in any way.


There used to be a practice in C++ of typedeffing the current class to This. I tried it, didn't find any startling benefit really.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜