// // example of inner class (showing access to enclosing object // and usefulness in implementing java.util.Enumeration) // import java.util.Enumeration ; import java.util.NoSuchElementException ; class MyClass2 { String[] someWords ; MyClass2(String[] someWords) { this.someWords = someWords ; } // method to return an "enumerator object" (object of type // java.util.Enumeration) Enumeration elements() { return new Enum() ; // return instance of inner class } // inner class for implementing java.util.Enumeration interface class Enum implements Enumeration { // within this class, have access to instance variable // someWords // instance variable position is index of next // element of someWords to print int position = 0 ; public boolean hasMoreElements() { return (position < someWords.length) ; } public Object nextElement() { if (position >= someWords.length) throw new NoSuchElementException() ; return someWords[position++] ; } } } public class Nest2 { public static void main(String[] args) { MyClass2 o1 = new MyClass2(new String[] { "two", "four", "six", "eight" } ) ; Enumeration e = o1.elements() ; while (e.hasMoreElements()) System.out.println(e.nextElement()) ; } }