// // example of local class (showing access to enclosing object // and usefulness in implementing java.util.Enumeration) // import java.util.Enumeration ; import java.util.NoSuchElementException ; class MyClass3 { String[] someWords ; MyClass3(String[] someWords) { this.someWords = someWords ; } // method to return an "enumerator object" (object of type // java.util.Enumeration) Enumeration elements() { // local class for implementing java.util.Enumeration 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++] ; } } return new Enum() ; // return new instance of local class } } public class Nest3 { public static void main(String[] args) { MyClass3 o1 = new MyClass3(new String[] { "two", "four", "six", "eight" } ) ; Enumeration e = o1.elements() ; while (e.hasMoreElements()) System.out.println(e.nextElement()) ; } }