Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
To help you get a handle on inner classes and what they are good for, let's revisit theStack
class. Suppose you want to add a feature to this class that lets another class enumerate over the elements in the stack using the interface defined injava.util.Enumeration
. This interface contains two method declarations:Thepublic boolean hasMoreElements(); public Object nextElement();Enumeration
interface defines the interface for a single loop over the elements:Ifwhile (hasMoreElements()) nextElement()Stack
implemented theEnumeration
interface itself, you could not restart the loop and you could not enumerate the contents more than once. Also, you couldn't allow two enumerations to happen simultaneously. SoStack
shouldn't implementEnumeration
. Rather, a helper class should do the work forStack
.The helper class must have access to the
Stack
's elements. It also must be able to access them directly because theStack
's public interface supports only LIFO access. This is where inner classes come in.Here's an implementation of
Stack
that defines a helper class (called an adapter class) for enumerating over its elements:Note that thepublic class Stack { private Vector items; ...//code for Stack's methods and constructors not shown... public Enumeration enumerator() { return new StackEnum(); } class StackEnum implements Enumeration { int currentItem = items.size() - 1; public boolean hasMoreElements() { return (currentItem >= 0); } public Object nextElement() { if (!hasMoreElements()) throw new NoSuchElementException(); else return items.elementAt(currentItem--); } } }StackEnum
class refers directly toStack
'sitems
instance variable.Inner classes are used primarily to implement adapter classes like the one shown in this example. If you plan on handling events from the AWT, then you'll want to know about using adapter classes because the event-handling mechanism in the AWT makes extensive use of them.
You can declare an inner class without naming it. Here's yet another version of the now-tiredStack
class, in this case using an anonymous class for its enumerator:Anonymous classes can make code difficult to read. You should limit their use to those classes that are very small (no more than a method or two) and whose use is well-understood (like the AWT event-handling adapter classes).public class Stack { private Vector items; ...//code for Stack's methods and constructors not shown... public Enumeration enumerator() { return new Enumeration() { int currentItem = items.size() - 1; public boolean hasMoreElements() { return (currentItem >= 0); } public Object nextElement() { if (!hasMoreElements()) throw new NoSuchElementException(); else return items.elementAt(currentItem--); } } } }
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.