View RSS Feed

Java Object

Enum Types

Rate this Entry
by , 04-26-2012 at 06:24 PM (655 Views)
An enum type is considered to be a type which possesses fixed constants set in its fields. Few common examples are the week days and compass directions (values of EAST, WEST, NORTH & SOUTH).
Enum type fields comprises of uppercase letters, as they are constants.

Enum keywords define the enum type, in Java programming language. For example, enum type of week days shall be specified like this:
Java Code:
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}
Anytime, enum types present the fixed constant sets. It contains Natural enum types, like data sets which consist of known values at the compiler time.

Following code will be showing that Day enum shall be used in this way:
Java Code:
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;
        }
    }

Submit "Enum Types" to Facebook Submit "Enum Types" to Digg Submit "Enum Types" to del.icio.us Submit "Enum Types" to StumbleUpon Submit "Enum Types" to Google

Tags: enum types Add / Edit Tags
Categories
Tutorial

Comments