views:

44

answers:

1
+2  Q: 

java.util.vector$1

enumeration e=vector.elements

But vector class does not implement Enumeration, then how comes it is returning Enumeration Reference. But e is refering to java.util.vector$1 . What is "Vector$1"???

+4  A: 

Vector$1 is an anonymous class. Vector.elements() creates a new instance of this anonymous class, which implements the Enumeration interface.

Here's the source code for Vector.elements() (badly formatted, as usual):

public Enumeration<E> elements() {
return new Enumeration<E>() {
    int count = 0;

    public boolean hasMoreElements() {
    return count < elementCount;
    }

    public E nextElement() {
    synchronized (Vector.this) {
        if (count < elementCount) {
        return (E)elementData[count++];
        }
    }
    throw new NoSuchElementException("Vector Enumeration");
    }
};
}
skaffman