Enum Types in Java

Enum Types in Java | Javamazon:



enuminjava
An enum type is a type whose fields consist of a fixed set of constants. Enum is a special type of classes. enum type put restriction on the instance values. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Because they are constants, the names of an enum type’s fields are in uppercase letters.
In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.
Here is some code that shows you how to use the Day enum defined above:
public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;

            case FRIDAY:
                System.out.println("Fridays are better.");
                break;

            case SATURDAY: case SUNDAY:
                System.out.println("Weekends are best.");
                break;

            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}
The output is:
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called anenum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.
Enum in java helps in overcoming these limitations ::
1. Missing type-safety :
First and foremost it is not type-safe. You are allowed to assign any port value which is not actual port value. Thus these constant can allow illegal values.
2. Missing Significant printing
Significant printing of the constant is missing from the way of defining constants using static final scheme.
3. I/O on final static Strings not easy.
For converting values from String to I/O operations is not easy.
4. No behaviour of its own
Constant Strings defined by static and final are only values there is no behaviour associated with it.
Benefits of using enum in java :
1. Unlike using array of Strings , we can use enums as case labels.
2. Enum in java are type safe.
3. If you are using lower version than java 7, then you can use enum in Switch statement.
4. We can easily add additional fields to the enum in java.
5. Enum in java uses its own namespace.
6. We can compare enums easily with ==.
7. Enum in java helps in reducing the bugs in our code.

0 comments:

Post a Comment