Enum is a list of constant
values assigned to a type. Enum can be declare outside or onside a class but
not in a method. Inside a class enum can’t be declare as final or abstract.
Enum declare outside the
class can’t be private, protected, static, final or abstract.
Points to remember for Java
Enum
1)
enum improves type safety.
2)
enum can be easily used in
switch.
3)
enum can be traversed.
4)
enum can have fields,
constructors, methods and constant class bodies.
5)
enum may implement many
interfaces but cannot extend any class because it internally extends Enum class.
6)
enum constructor can never be
invoked directly in code. It will always called automatically when an enums is
initialized.
7)
Generic can’t be applied on
enum.
8)
enum can have abstract method
but we have to provide the implementation of abstract method otherwise it will
give compile time error.
9)
All instance of enums are
serilazable by default.
10) Enums enable you to define a new data type. For
example, If you create an Enum 'Days' you can declare a variable of type
'Days'.
Java Program for Enum :
/**
* Class Enums to sample of enum
DAY_OF_WEEK
* @author Manoj
*/
public class Enums {
public static void main(String[] args) {
for(DAY_OF_WEEK day:DAY_OF_WEEK.values()) {
System.out.println(day.name()+" | "+day.getNumericValue());
}
}
enum DAY_OF_WEEK {
MON(1),TUE(2),WED(3),THU(4),FRI(5),SAT(6),SUN(0);
private int numericValue;
DAY_OF_WEEK(int numericValue) {
this.numericValue = numericValue;
}
public int getNumericValue(){
return numericValue;
}
}
}
Java Program for enum containing abstract method:
/**
* Class Enums to sample of enum
DAY_OF_WEEK with abstract method and it's
* implementaion
* @author Manoj
*/
public class Enums {
public static void main(String[] args) {
for (DAY_OF_WEEK day : DAY_OF_WEEK.values()) {
System.out.println(day.name()
+ " | " + day.getNumericValue());
}
}
enum DAY_OF_WEEK {
MON {
public int getNumericValue() {
return 1;
}
},
TUE {
public int getNumericValue() {
return 2;
}
},
WED {
public int getNumericValue() {
return 3;
}
},
THU {
public int getNumericValue() {
return 4;
}
},
FRI {
public int getNumericValue() {
return 5;
}
},
SAT {
public int getNumericValue() {
return 6;
}
},
SUN {
public int getNumericValue() {
return 0;
}
};
public abstract int getNumericValue();
}
}
enumMap:
1.
All key used in EnumMap must be from same enum type.
2.
EnumMap is a ordered collection and it’s maintain a natural
order.
3.
NULL keys are not allowed.
4.
EnumMap give better performance than HashMap.
Code snippet –
/**
* Class Enum contains EnumMap
containing keys of type DAY_OF_WEEK
* @author Manoj
*/
public class Enums {
public static void main(String[] args) {
EnumMap<DAY_OF_WEEK, String> enumMap = new EnumMap<>(DAY_OF_WEEK.class);
for (DAY_OF_WEEK day : DAY_OF_WEEK.values()) {
System.out.println(day.name() + " | " + day.getNumericValue());
enumMap.put(day, "hi");
}
enumMap.forEach((key, value) -> System.out.println(key+" |
"+value));
}
}
How to
control the marshalling of enum ouput:-
public enum ThreadState {
NEW(0), RUNNABLE(1), BLOCKED(2), WAITING(3), TIMED_WAITING(4), TERMINATED(5);
private Integer state;
ThreadState(Integer state) {
this.state = state;
}
/**
* To
control the marshalling of output of enum use @JsonValue here
*
getState method will represent the actual marshalling of enum.
*/
@JsonValue
public Integer getState() {
return state;
}
}
good resource for enum . thanks for summarise in short
ReplyDeleteThanks Dileep.
DeleteNice information, very usefull thanks
ReplyDelete