Enums, whenever I come across this term, I don't know the practical use and power of Enums. I have tried explaining it in this post.
Definition - Finite set of symbolic names and their values. These symbolic names are called as enum constants.
In more technical way, An enum is used as any other reference type. Enum constants are infact final, static variables of the enum type and they are implicitly initialized with objects of the enum type when the enum type is loaded at the run time.
I know it went over your head, same happened to me when I first read above lines. Below mentioned statement will describe it.
enum test {A, B,C}
public static final A = new test()
About the use of enums:
1. We can pass enum constants as an arguments.
2. We can declare reference whose type is an enum type.
3. We cannot pass a type of value other than the enum type.
4. We cannot create a new constant of enum type.
Lets take an example which will cover these points
// File name - MachineState.java
public enum MachineState {BUSY, IDLE, BLOCKED}
// File name - Machine.java
public class Machine
{
private MachineState state; // 2nd Point Justified
public void setState(MachineState state)
{
this.state = state;
}
public MachineState getState()
{
return this.state;
}
}
public class MachineClient
{
public static void main(String args[])
{
Machine machine = new Machine();
machine.setState(MachineState.IDLE); //1st Point Justified
// machine.setState(1); compile time error // 3rd Point Justified
MachineState state = machine.getState();
system.out.println(state);
//MachineState newState = new MachineState(); compile time error // 4th Point Justified
}
}
Declaring Enum constructors and Members:
An Enum declaration is a special kind of reference type declaration. It can declare constructors and other members as in an ordinary class but the enum constants must be declared before any other declarations. Lets take an example:
enum abc
{
HI("hi"), BYE("bye");
abc(String s)
{
this.wish = s;
}
private String wish;
public String getWish()
{
return wish;
}
public void setWish(String wish)
{
this.wish = wish;
}
public class Greet
{
public static void main(String args[])
{
String e;
e = abc.Bye.getWish();
system.out.println(e);
}
}
}
Implicit Static methods for Enum Types:
1. Static EnumTypeName[] values()
e.g. abc a[] = abc.values(); --> HI , BYE
2. Static EnumTypeName valueOf(String name)
e.g. abc o = abc.valueOf("HI") --> hi
Enum as a Map:
Making enum a reference type that can contain fixed set of constants has led to efficient Map implementation like EnumMap and Set implementation like EnumSet (JDK classes).
From javadoc of EnumMap :
A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
EnumMap combines richness and type safety of Map with the speed of an array.
Having said that, I tried to solve a problem using enums instead of Map. Problem was - Print a number in word say if user inputs a number we have to display that in word. Below mentioned is the solution:
public class example
{
public static void main(String args[])
{
String convertDigitToWord = word.lookup(5);
System.out.println(convertDigitToWord);
}
public enum word
{
ZERO(0,"ZERO"),
ONE(1,"ONE"),
TWO(2,"TWO"),
THREE(3,"THREE"),
FOUR(4,"FOUR"),
FIVE(5,"FIVE");
private int digit;
private String digitWord;
word(int digit, String digitWord)
{
this.digit = digit;
this.digitWord = digitWord;
}
public static String lookup(int digit)
{
for(word w : word.values())
{
if(w.getDigit() == digit)
{
return w.getDigitWord();
}
}
return null;
}
public int getDigit()
{
return digit;
}
public void setDigit(int digit)
{
this.digit = digit;
}
public String getDigitWord()
{
return digitWord;
}
public void setDigitWord(String digitWord)
{
this.digitWord = digitWord;
}
}
}
I hope this will help you to understand enums easily. Requesting vistors to share their knowledge about enums so that I can include that also in this post. Good Luck and happy learning !!