Enum was introduced in
java with jdk5 release. It is one of the most overlooked feautures by java
developers. So lets try to understand what is an enum and what are the features
and advantage of using enum.Enum in java is a set
of named constants. Enum is declared with the keyword enum.
enum Days {
SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;
}
The above enum is a set
of named constants representing the days of
a week. Enums are internally represented as a class. So the above enum
will roughly translated to below code:-
Class Days {
public static final SUNDAY = new Days();
public static final MONDAY = new Days();
public static final TUESDAY = new Days();
public
static final WEDNESDAY = new Days();
public
static final THURSDAY = new Days();
public
static final FRIDAY = new Days();
public
static final SATURDAY = new Days();
}
So every named constant
in an enum is an object of the same enum type and are implicitly public, static
and final. Enum in java are more
powerful as it can contain variable, methods and consturctors unlike the enum
in other programming language. Some of the features of enum are as below:
1.All named constants
in an enum are implictly public, static and final are represent an instance of
same enum type.
2.enum in java can not
extend any other class as every enum in java implicitly extends java.lang.Enum
class.
3.enums can not be
extended by any other class as enums in java are implicitly final and so can
not be extened.
4.enums can implement
any number of interfaces.
5.enum constants are
final but it can also contain other variables apart from named constants.
6.enums can be used in
swithch statement.
7.enum constructors are
always private.
8. we cant create
instance of enum using new operator.
9. We can declare abstract
methods in java enum, then all the enum fields must implement the abstract
method
Lets try to demonstrate
all this through code example:
Lets declare an enum ThreadStatus:
public enum
ThreadStatus {
START, RUNNING, WAITING, DEAD;
}
Lets also add priority
instance variable for the above enum and assign some value for each enum
constant.lets also declare a constructor.
public enum
ThreadStatus {
START(1), RUNNING(2), WAITING(3), DEAD(4);
private int priority;
private ThreadStatus(int priority){
this.priority = priority;
}
}
Lets also add an abstract
method called getDetail(). As the method is abstract all enum constants should
implement this abstract method. Also add getters and setters for priority
instance variable. The final enum code will look like this:-
public enum
ThreadStatus {
START(1) {
@Override
public String getDetail() {
return "Starting";
}
}, RUNNING(2) {
@Override
public String getDetail() {
return "Running";
}
}, WAITING(3) {
@Override
public String getDetail() {
return "Waiting";
}
}, DEAD(4) {
@Override
public String getDetail() {
return "Dead";
}
};
private int priority;
private ThreadStatus(int priority){
this.priority = priority;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public abstract String getDetail();
}
Lets demonstrate the
use of enum through example:
public class
ThreadStatusEnumDemo {
/**
* @param
args
*/
public static void main(String[] args) {
ThreadStatus[] status =
ThreadStatus.values();
for(ThreadStatus s : status){
System.out.println("Thread
Priority:" +s.getPriority());
System.out.println("Thread
Details:" +s.getDetail());
}
//change the priority of thread
statuses
ThreadStatus.STARTING.setPriority(5);
ThreadStatus.RUNNING.setPriority(10);
ThreadStatus.WAITING.setPriority(15);
ThreadStatus.DEAD.setPriority(20);
System.out.println("***New
priority of threads****");
for(ThreadStatus s : status){
System.out.println("Thread
Priority:" +s.getPriority());
}
//demonstrate the use of switch
System.out.println("****switch
demo*****");
for(ThreadStatus s : status){
switch(s){
case STARTING:
System.out.println("THREAD
IS STARTING");
break;
case RUNNING:
System.out.println("THREAD
IS RUNNING");
break;
case WAITING:
System.out.println("THREAD
IS WAITING");
break;
case DEAD:
System.out.println("THREAD
IS DEAD");
break;
default:
System.out.println("invalid
thread status");
}
}
}
}
No comments:
Post a Comment