Because the Java language lacks delegates, anonymous classes are prevalent as a syntactic replacement. Non-static nested classes are also often used in the language, a feature which is conspicuously absent from C#, albeit far less necessary with that language.
This brings me to the following language caveat. This may seem a contrived example, but it's a simplification of an actual issue I've encountered in the last few days. Suppose you have a generic base class, call it BaseClass<U>, which you extend with an anonymous class. Lets also assume that the extended class spawns a thread that needs to access the BaseClass<U> state:
class BaseClass<U> { void getState() {} } class Test { public void test() { final BaseClass<String> instance = new BaseClass<String>() { public void invokeStatefulThread() { // Create our runnable final Runnable threadCode = new Runnable() { public void run() { /* 1 */ getState(); /* 2 */ this.getState(); /* 3 */ super.getState(); /* 4 */ BaseClass.this.getState(); /* 5 */ BaseClass<String>.this.getState(); } }; new Thread( threadCode ).start(); } }; instance.invokeStatefulThread(); } }
I'll spare you the guessing game. Here's what happens with each of the five invocations of getState():
The Java language specification section on "qualified this" is very brief and does not mention generics at all (does "class name" include bounded type parameters?). Oddly enough, moving the class declaration outside of the test method actually lets 4 compile -- if there's a clue there, I haven't figured it out yet.
I still haven't found a syntactically-correct way to access BaseClass<string>.this, other than placing it in a temporary variable outside of the Runnable declaration. I searched yesterday for a couple of hours with no obvious solution in sight. Ideas are more than welcome!...
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u