- Version prior to JDK 5 lacked on feature that many programmers felt was needed : “Enumeration”.
- An enumeration is a list of named constants.
- In language such as c++, enumerations are simply lists of named integer constants.
- In Java, an enumeration defines a class type. By example, in java, an enumerations into classes, the concept of the enumeration is greatly expanded.
For example, in java, an enumeration can have constructors, methods, and instance variables.
java.lang.Enum Implementation
C:\>javap java.lang.Enum
public abstract class java.lang.Enum<E extends java.lang.Enum<E>> implements java.lang.Comparable<E>, java.io.Serializable
{
public final java.lang.String name();
public final int ordinal();
protected java.lang.Enum(java.lang.String, int);
public java.lang.String toString();
public final boolean equals(java.lang.Object);
public final int hashCode();
protected final java.lang.Object clone() throws java.lang.CloneNotSupportedException;
public final int compareTo(E);
public final java.lang.Class<E> getDeclaringClass();
public static <T extends java/lang/Enum<T>> T valueOf(java.lang.Class<T>, java.lang.String);
protected final void finalize();
public int compareTo(java.lang.Object);
}
C:\>
Let us have an Example
EnumDemo.java
enum Fruits
{
mango, banana, pine_apple, water_melon,Orange
}
class EnumDemo{
public static void main(String arg[])
{
Fruits fr;
fr=Fruits.mango;
System.out.println("value of fr: " +fr);
fr=Fruits.banana;
if(fr == Fruits.banana)
{
System.out.println("fr contains banana");
}
switch (fr)
{
case mango:
System.out.println("mango is green");
break;
case banana:
System.out.println("banana is yellow");
break;
case pine_apple:
System.out.println("pine_apple is red");
break;
case water_melon:
System.out.println("water_melon is combination of red and yellow");
break;
case Orange:
System.out.println("orange is yellow");
break;
}
}
}
Fruits Enum Declaration.
C:\>javap Fruits
Compiled from "EnumDemo.java"
final class Fruits extends java.lang.Enum<Fruits> {
public static final Fruits mango;
public static final Fruits banana;
public static final Fruits pine_apple;
public static final Fruits water_melon;
public static final Fruits Orange;
public static Fruits[] values();
public static Fruits valueOf(java.lang.String);
static {};
}
All enumerations automatically contain two predefined methods:
- public static enum-type[] values()
- public static enum-type valueOf(String str).
Output
C:\>java EnumDemo
value of fr: mango
fr contains banana
banana is yellow
EnumDemo1.java
// Use the built-in enumeration methods.
// An enumeration of apple varieties.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo1
{
public static void main(String args[])
{
Apple ap;
System.out.println("Here are all Apple constants");
// use values()
Apple allapples[] = Apple.values();
for(Apple a : allapples)
System.out.println(a);
System.out.println();
// use valueOf()
ap = Apple.valueOf("Winesap");
System.out.println("ap contains " + ap);
}
}
Output
c:\>javac EnumDemo1.java
c:\>java EnumDemo1
Here are all Apple constants
Jonathan
GoldenDel
RedDel
Winesap
Cortland
ap contains Winesap
EnumDemo2.java
enum Apple {
Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8);
private int price; // price of each apple
// Constructor
Apple(int p) { price = p; }
int getPrice() { return price; }
}
class EnumDemo2 {
public static void main(String args[])
{
Apple ap;
// Display price of Winesap.
System.out.println("Winesap costs " + Apple.Winesap.getPrice() + " cents.\n");
// Display all apples and prices.
System.out.println("All apple prices:");
for(Apple ap : Apple.values())
System.out.println(ap + " costs " + ap.getPrice() + " cents.");
}
}
Output
c:\>javac EnumDemo2.java
c:\>java EnumDemo2
Winesap costs 15 cents.
All apple prices:
Jonathan costs 10 cents.
GoldenDel costs 9 cents.
RedDel costs 12 cents.
Winesap costs 15 cents.
Cortland costs 8 cents.
EnumDemo3.java
// Use an enum constructor.
enum Apple {
Jonathan(10), GoldenDel(9), RedDel, Winesap(15), Cortland(8);
private int price; // price of each apple
// Constructor
Apple(int p) { price = p; }
// Overloaded constructor
Apple() { price = -1; }
int getPrice() { return price; }
}
class EnumDemo3
{
public static void main(String arg[])
{
Apple ap;
ap=Apple.RedDel;
System.out.println("Price of RedDel : "+ap.RedDel.getPrice());
ap=Apple.Winesap;
System.out.println("Price of Winesap : "+ap.Winesap.getPrice());
}
}
Output.
c:\>javac EnumDemo3.java
c:\>java EnumDemo3
Price of RedDel : -1
Price of Winesap : 15
EnumDemo4.java
// An enumeration of apple varieties.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo4 {
public static void main(String args[])
{
Apple ap, ap2, ap3;
// Obtain all ordinal values using ordinal().
System.out.println("Here are all apple constants" + " and their ordinal values: ");
for(Apple a : Apple.values())
System.out.println(a + " " + a.ordinal());
ap = Apple.RedDel;
ap2 = Apple.GoldenDel;
ap3 = Apple.RedDel;
System.out.print("\n");
// Demonstrate compareTo() and equals()
if(ap.compareTo(ap2) < 0)
System.out.println(ap + " comes before " + ap2);
if(ap.compareTo(ap2) > 0)
System.out.println(ap2 + " comes before " + ap);
if(ap.compareTo(ap3) == 0)
System.out.println(ap + " equals " + ap3);
System.out.print("\n");
if(ap.equals(ap2))
System.out.println("Error!");
if(ap.equals(ap3))
System.out.println(ap + " equals " + ap3);
if(ap == ap3)
System.out.println(ap + " == " + ap3);
}
}
Output
c:\>javac EnumDemo4.java
c:\>java EnumDemo4
Here are all apple constants and their ordinal values:
Jonathan 0
GoldenDel 1
RedDel 2
Winesap 3
Cortland 4
GoldenDel comes before RedDel
RedDel equals RedDel
RedDel equals RedDel
RedDel == RedDel
Recent Comments