Enumerated types are the collection of values. A field of enumerated types can only assigned the values which are present in the enumerated types. The standard way to represent an enumerated type in earlier version of Java was the int Enum pattern. For example:-
public static final int DAY_SUNDAY = 0;
public static final int DAY_MONDAY = 1;
This pattern has many problems, such as:
- Since a SUNDAY, MONDAY is just an int you can pass in any other int value where a days is required, or add two days together which makes no sense. This also means that int enums are not Type Safe.
- You must prefix constants of an int enum with a string to avoid collisions with other int enum types.
- Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined.
- Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.
In 5.0, the Java™ programming language gets linguistic support for enumerated types. These enums in Java looks just like C, C++, and C# Enums. The following is the syntax to declare the Enums in Java.
public enum enumName {Value1,..........ValueN;}
Example:-
The below example will demonstrate the use of Enums.
package com.visualbuilder;
import java.util.ArrayList;
public class EnumsExamples {
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
public static void main(String[] args) {
System.out.println(Day.SUNDAY);
}
}
Output:-
The above program will print SUNDAY on console.
Java Discussion
- - Java web application
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver





