Question

Is there any difference between enum datatype and Enumeration Interface. I have become confused between the two.

I got my answer that they aren't related but that brings me to another question.

We cannot instantiate interface . So what is the significance of this line

Enumeration days = dayNames.elements();

Heres the complete code containing that line

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationTester {

   public static void main(String args[]) {
      Enumeration days;
      Vector dayNames = new Vector();
      dayNames.add("Sunday");
      dayNames.add("Monday");
      dayNames.add("Tuesday");
      dayNames.add("Wednesday");
      dayNames.add("Thursday");
      dayNames.add("Friday");
      dayNames.add("Saturday");
      days = dayNames.elements();
      while (days.hasMoreElements()){
         System.out.println(days.nextElement()); 
      }
   }
}
Was it helpful?

Solution

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.

OTHER TIPS

To be short, Enumeration is legacy Iterator and Enum is a data type.

Enums are instance controlled classes in java. You can predefine the different flavors, a type can support. For ex.

public enum Direction {
    EAST, WEST, NORTH, SOUTH
} 

defines a type named Direction which can be of 4 types. It's not permitted to instantiate these types from outside their type definition. You can check more on Enum here.

While Enumeration was the old way to iterate through a collection. It has two methods nextElement and hasMoreElements which are more like next and hasNext methods of Iterator interface. In old APIs like Servlet we can still see some methods which return Enumeration. For ex.

Java.util.Enumeration<String> paramNames = request.getParameterNames();

as per the oracle docs

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. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

example of enum

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;

            case FRIDAY:
                System.out.println("Fridays are better.");
                break;

            case SATURDAY: case SUNDAY:
                System.out.println("Weekends are best.");
                break;

            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

The output is:

Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.

Enumeration

The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.

example of enumeration

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationTester {

   public static void main(String args[]) {
      Enumeration days;
      Vector dayNames = new Vector();
      dayNames.add("Sunday");
      dayNames.add("Monday");
      dayNames.add("Tuesday");
      dayNames.add("Wednesday");
      dayNames.add("Thursday");
      dayNames.add("Friday");
      dayNames.add("Saturday");
      days = dayNames.elements();
      while (days.hasMoreElements()){
         System.out.println(days.nextElement()); 
      }
   }
}

output

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top