Enumeration is an interface : An object that implements the Enumeration
interface generates a series of elements, one at a time. Successive calls to the nextElement
method return successive elements of the series.
For example, to print all elements of a Vector<E> v
:
for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
System.out.println(e.nextElement());
enum is a data type: An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
For example, you would specify a days-of-the-week enum
type as:
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args)
{
System.out.ptintln(Day.SUNDAY); // print SUNDAY
}
Your Second Question:
We cannot instantiate interface . So what is the significance of this line
Enumeration days = dayNames.elements();
dayNames
is a Vector
, a collection just like List
. (There are differences, but that is beyond the scope of the question.). However, when daynames.elements()
is called, it returns an enumeration of the components of vector daynames
. The returned Enumeration object will generate all items added to this vector. The first item generated is the item at index 0
, then the item at index 1
, and so on.