Michael Rutherfurd
Michael Rutherfurd

Reputation: 14045

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" returns 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?

Upvotes: 0

Views: 144

Answers (2)

hvgotcodes
hvgotcodes

Reputation: 120178

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.

Upvotes: 2

user207421
user207421

Reputation: 310859

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.

Upvotes: 0

Related Questions