Enumerations, commonly known as enums can be considered as a group of predefined constant values.
Enum values are implicitly public, static and final. They can be used as variable types as well as cases in the switch constructs,
An enum keyword is used to represent enum type.
public abstract class Enum<E extends Enum<E>>
extends Object implements Comparable<E>, Serializable
It can define instance variables and methods. A constructor should be added to enumeration to initialize its instance variables and must have default or private access. Declaration of the enum values can be invoked outside enum.
The restriction imposed on the enum constructor ensures that enum will represent fixed values. Let us see the below example to understand the concept.
package coderoversduo;
public enum Weather{
HOT("Summer"),
COLD("Winter"),
RAINY("Monsoon");
private String season;
private Weather(String season){
this.season =season;
}
public String getSeason(){
return season;
}
}
When we usually know all the probable values for a variable before program execution, we opt for enums.
The below code snippet describes how to create an enum. Rating is the enum name here and NO_RATING, ONE_STAR, TWO_STAR, THREE_STAR, FOUR_STAR, FIVE_STAR are the predefined values.
//Declaring an Enum
public enum Rating{
NO_RATING, ONE_STAR, TWO_STAR,
THREE_STAR, FOUR_STAR, FIVE_STAR
}
//Accessing an Enum
Rating productRating = Rating.FIVE_STAR;
String name() - Returns the name of this enum constant, exactly as declared in its enum declaration.
int ordinal() - Returns the position of the enumeration constant in its enum declaration. The initial constant is assigned an ordinal of zero.
boolean equals(Object other) - Returns true if the specified object is equal to this enum constant.
int compareTo(E object) - Enum constants are only comparable to other enum constants of the same enum type. So compareTo() method compares this enum with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Enum<T>> T valueOf(Class<T> enumType,String name) - Returns the enum constant of the specified enum type with the specified name. Additional whitespaceare nit allowed.
String toString() - Returns the name of this enum constant, as contained in the declaration.
enum type can be passed as an argument to switch statement. The example below demonstrates the same.
package coderoversduo;
public class JavaEnum {
public enum Rating {
NO_RATING, ONE_STAR, TWO_STAR, THREE_STAR, FOUR_STAR, FIVE_STAR
}
public static void main(String[] args) {
Rating productRating = Rating.FOUR_STAR;
switch (productRating) {
case FIVE_STAR:
System.out.println("Best Product");
break;
case FOUR_STAR:
System.out.println("Good Product");
break;
case THREE_STAR:
System.out.println("Average Product");
break;
case TWO_STAR:
System.out.println("Bad Product");
break;
case ONE_STAR:
System.out.println("Worst Product");
break;
default:
System.out.println("Unrated Product.");
}
}
}
OUTPUT: Good Product
Adding a concrete method in enum is equivalent to the addition of methods in any other class.
package coderoversduo;
public class JavaEnum {
public enum Rating {
NO_RATING(0), ONE_STAR(1), TWO_STAR(2), THREE_STAR(3), FOUR_STAR(4), FIVE_STAR(5);
private final int ratingCode;
Rating(int ratingCode) {
this.ratingCode = ratingCode;
}
public int getratingCode() {
return this.ratingCode;
}
}
public static void main(String[] args) {
System.out.println(Rating.FIVE_STAR.ratingCode);
System.out.println(Rating.FOUR_STAR.ratingCode);
System.out.println(Rating.THREE_STAR.ratingCode);
System.out.println(Rating.TWO_STAR.ratingCode);
System.out.println(Rating.ONE_STAR.ratingCode);
}
}
OUTPUT :
5
4
3
2
1
The EnumSet is a specialized Set implementation that holds enums more efficiently than the normal Java Set implementations.
EnumSet<Rating> enumSet = EnumSet.of(Rating.FIVE_STAR, Rating.FOUR_STAR);
It also supports special Java Map implementation which can use Java enum instances as the map keys.
EnumMap<Rating, Integer> enumMap = new EnumMap<Rating, Integer>(Rating.class);
enumMap.put(Rating.FIVE_STAR, 5);
enumMap.put(Rating.FOUR_STAR, 4);