Java Reference
In-Depth Information
neededtoobtainareferencetoits EnclosingClass instance,itwouldspecify En-
closingClass.this .
Anonymous Classes
An anonymous class isaclasswithoutaname.Furthermore,itisnotamemberofitsen-
extension of a class or as an anonymous implementation of an interface) and instanti-
atedanyplacewhereitislegaltospecifyanexpression. Listing3-10 demonstrates an
anonymous class declaration and instantiation.
Listing 3-10. Declaring and instantiating an anonymous class that extends a class
abstract class Speaker
abstract void speak();
class ACDemo
public static void main(final String[] args)
new Speaker()
String msg = (args.length == 1) ? args[0] : "noth-
ing to say";
void speak()
Listing3-10 introducesanabstractclassnamed Speaker andaconcreteclassnamed
ACDemo .Thelatterclass's main() methoddeclaresananonymousclassthatextends
Search WWH ::

Custom Search