All About Interface Enumeration in Java

Enumeration in Java
Enumeration in Java

Enumeration in Java:

Even though java supports the eight predefined built in primitive types and many non primitive types,but still can not cover all possible types of classes or situation.To support this java provides enum(Short of Enumerated types).It is a special type of class.An enum is type of fixed list of possible values.The values needs to be defined when the enum is created.An enum is not primitive data type.Each enumerated type constant is a public final static member variables.The values of the variable is an object belonging to the enumerated class.There is one such object for each enumerated type constant and there are the only objects of the class that can ever be created.It is really thse objects that represent the possible values of the enumerated type.An object that implements the Enumeration interface generates a series of elements,one at a time.The enumerated type constants are actually variables that refer to these objects. Successive calls to the nextElement() method which returns the successive elements of the Enumeration series, methods are provided to enumerate through the elements of a vector, the key of a hashtable and the values in a hashtable.Enumerations are also used to specify the input streams from a sequenceInputStream.

Any class that contains methods of these names(written below) is said to implement the Enumeration interface. We can say StringTokenizer class implements this interface.The enum typevalues are referred as enum constants hence the values are made up with uppercase letters. This is a style but not syntax.

for printing all elements of a vector myVec:

interface Enumeration{
public boolean hasMoreElements();
public Object nextElement();
for(Enumeration e=myVec.elements();
The vector,Hashtable and HashMap classes all have an elements method which returns an Enumeration Objects.The enum definition should not be part of any function including main method. This have to be defined outside of the method.

The interface looks below:

public interface java.util.Enumeration{
public abstruct boolean hasMoreElements();
//returns true if the enumeration contains more elements and returns false 
//if the enumeration contains no elements
public abstruct Object nextElement()
//returns the next element of this enumeration. It can throw NoSuchElementException 

//if there is not element present.


An Example:

Vector v=new Vector();

Enumeration enum=v.elements();
while (enum.hasMoreEmenets())
String s=(String)enum.nextElement();

An enum is technically a class so the enum values are objects of that class.They also contain methods called ordinal(). When used with an enum,the ordinal() method returns the position of the enum object in the list.

enum Browser{IE,FF,Chrome}
Browser browserIE=Browser.IE;
Browser browserFF=Browser.FF;
Browser browserChrome=Browser.Chrome;
Browser.FF.ordinal();//returns 2
A unit of collection of data items is called data structure.Example List(a sequence of items)
enhanced for loop can be used as below:

for((enum-type-name)(variable name):(enum-type-name).values()){{
enum Days{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}
for(Days d:Days.values())
Enhanced for loop is to do something "for each" item in a data structure,it is often called for each for each Days d in Days.values()
: menas in.
The method clone() duplicates the class vector not the objects it holds.Since clone is a method of Object class,many classes are cloneable.

Another Example:

class EnumeratorTest{
public static void main(String args[]){
Vector v=new Vector();
v.addElement(new file("."));
v.addElement(new Vector());
v.addElement(new EnumeratorTest());
v.addElement(new Date());
//class object represents a class itself.

Hashtable h=new Hashtable();
h.put(new Integer(1),new Thread());
h.put(new Integer(2),new ProtocolException().getClass().getSuperClass());
h.put(new Integer(3),v.clone());

Note that the only thing common between containers is that they contain only class object.Now if we to keep a specific type in that case Enumeration is the key.However we need to downcast Object to the particular type.
All About Interface Enumeration in Java All About Interface Enumeration in Java Reviewed by Animesh Chatterjee on September 14, 2018 Rating: 5

No comments:

Powered by Blogger.