Enum in Java is a keyword. It is used to represent a fixed number of predefined values. Enumeration (Enum) in Java was introduced in JDK 1.5.

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data.

Best example of enum is to define days of a week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY), directions (NORTH, SOUTH, EAST and WEST) etc.

It can be replicated with public static final constant.

Simple enum example:

Now if you want to display some extra information with your predefined values which gives meaning to your enum.

Let’s take above example. If you want to display short form of days with enum i.e. MONDAY should be MON, SUNDAY should be SUN.

Key Point : You need to create private constuctor of value whichever you have declared in enum values. Same could be get by providing getters.

You can use them in comparisons using the equals() or compareTo() methods.

The Java Compiler automatically generates a static method for each enum, called values. This method returns an array of all constants defined inside the enum:

Reference Docs:
Oracle EnumMap Reference
It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *