//
// example of anonymous class (showing access to enclosing object 
//      and usefulness in implementing java.util.Enumeration)
//

import java.util.Enumeration ;
import java.util.NoSuchElementException ;

class MyClass4 {

        String[] someWords ;

        MyClass4(String[] someWords) {
                this.someWords = someWords ;
        }

        // method to return an "enumerator object" (object of type
        //      java.util.Enumeration)

        Enumeration elements() {

                return new Enumeration() {

                        // anonymous class for implementing
                        //      java.util.Enumeration -- note
                        //      syntax ("new" plus interface name)

                        // 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++] ;
                        }
                } ;     // "gotcha" here -- this semicolon needed to
                        //      end "new"

        }

}

public class Nest4 {

        public static void main(String[] args) {

                MyClass4 o1 = new MyClass4(new String[] 
                        { "two", "four", "six", "eight" } ) ;

                Enumeration e = o1.elements() ;
                while (e.hasMoreElements()) 
                        System.out.println(e.nextElement()) ;
        }
}