I have seen Java code that says something like:
SomeClass.this.someMethod(someArg);
Blah(AnotherClass.class);
Blah(YAClass.this);
What do “this” and “class” mean here? I am used to them as keywords to refer to the current object and to define a class, but this is different. My Java book and online searches have not yielded any explanation.
SomeClass.this/YAClass.this– thethisreference of an inner class’ enclosingSomeClass/YAClassclass.(You need to be very careful which
thisyou get particularly when dealing with operations that could be applied to anyObjectreference. A common case is syncronising onthisin an inner class, when the code should be synchronising on the outer instance (a better approach in this case is to use an explicit lock object).)AnotherClass.class– thejava.lang.Classobject for theAnotherClassclass. Prior to Java 1.5 this was implemented usingClass.forName(initialising the class); from 1.5 theldcbytecode has been extended for direct support.Both were introduced in Java 1.1.