Inner ClassesInner classesFour kinds of inner classesMember classesMember classes IIAnonymous inner classesExample anonymous inner classStatic member classesLocal classesSummaryThe EndJan 15, 2019Inner Classes2Inner classesAll the classes so far have been “top level”It is possible (and useful) to define a class inside another classThe usual access modifiers (public, protected, private) can be usedInner classes were not in Java 1.0They had to be added in laterAs a result, inner classes are not as well done as some other aspects of the language3Four kinds of inner classesMember classesSimple and usefulAnonymous classesUseful, but syntax is ugly Static member classes (not too useful)Local classes (not too useful)Every class compiles to a separate .class fileInner classes compile to files with a $ in their names4Member classesA member class is an “ordinary” inner class class Outer { int n; class Inner { int ten = 10; void setNToTen( ) { n = ten; } } void setN ( ) { new Inner( ).setNToTen( ); }}5Member classes IIMember classes are often used to handle events: Button b = new Button ("Click Me");b.addActionListener (new Clicker( ));…class Clicker implements ActionListener { … }A member class can access the variables of the enclosing classThis is what makes them so useful!Member classes are very easyDeclare them where you would declare a field or a method6Anonymous inner classesAnonymous inner classes are convenient for short code (typically a single method) b.addActionListener(anonymous inner class);The anonymous inner class can be either: new Superclass(args) { body } or new Interface() { body }Notice that no class name is given--only the name of the superclass or interfaceIf it had a name, it wouldn’t be anonymous, now would it?The args are arguments to the superclass’s constructor (interfaces don’t have constructors)7Example anonymous inner classAn ActionListener is a Java-supplied interface for listening to Buttons and some other thingsThe format (from the previous slide) is new Interface () { body } b.addActionListener (new ActionListener( ) { public void actionPerformed (ActionEvent e) { System.out.println (“Ouch!”);} } ) ;Like member classes, anonymous inner classes have full access to the fields and methods of the containing class8Static member classesstatic class StaticMember { … }A static member class can access only static variables of the outer classA static member class isn't “really” an inner class, but a top-level class that happens to be written inside another classStatic member classes are not too useful9Local classesA local class is a class defined inside a methodLike any other local declarations, the class declaration is available only within that methodHowever, objects created from that local class can “escape” the class by being assigned to nonlocal variablesBecause its instances may exist after the method exits, code in the local class cannot access variables declared in the method unless they are declared finalThis makes them practically uselessThere are many other restrictions on local classes10SummaryMember classesAn ordinary class, just defined within anotherHas full access to the variables of the enclosing classAnonymous classesUseful for short Listeners used in only one place Has full access to the variables of the enclosing classStatic member classesDefined inside another class, but acts like an outer classLocal classesDefined within a methodCan access final variables in the enclosing class11The
View Full Document