Saturday, March 31, 2007

A new usage of this() !

All Java developers know the this()-method, which is used to call a constructor of a class within another one. It is the only Java-method with a return value constrained to the actual class (a.k.a. self-type).

Imagine if we could use the parameterless version (even if there is no such constructor) within a method. As this, it would return a reference of the class, but virtually typed.

What about the benefits?
This simple, but powerful language extension would allow virtual calls to static methods!


Let's take a look at this example.



Without the new this()-method, one would have to override every method calling the static method, which we want to be virtual. Here, one would have to remove all comment to maintain the output:

One: static(0) - virtual(1)
Two: static(0) - virtual(2)


What do you think, is it worth to submit a RFE?
At least no new keyword or anything else have to introduced and the meaning is widely consistent with the constructor.