-7

SO I do not quite get what an enum is, is it like an array, that can have all different data types, and can also have other array like objects inside it? Sorry if this is not in a good format, but this is the only decent plkace to get answers.

Chris London
  • 81
  • 1
  • 9
  • You can probably find help elsewhere. Which specific documentation have you read, and which specific part(s) of the documentation do you need help with? – Anderson Green Oct 05 '13 at 20:47
  • I suggest you read the Java tutorials again. – Peter Lawrey Oct 05 '13 at 20:49
  • possible duplicate of [Why and what for: java enum](http://stackoverflow.com/questions/4709175/why-and-what-for-java-enum) – Anderson Green Oct 05 '13 at 20:49
  • Simple way to understand enums is to see its insides. You can do it by using `javap -c YourEnumClass`. This way you will see that each enum is class that extends `java.lang.Enum` and have static final fields with instances of this class (which are enum values). Only additional things that compiler creates are methods: `ordinal` and `values`. – Pshemo Oct 05 '13 at 21:05

3 Answers3

2

Quoting the official Java documentation itself here:

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73
1

create enum with file name EnumDay.java

public enum EnumDay {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,NAVEED
}

and testclass with file name EnumTest.java

 public class EnumTest {
EnumDay day;

public EnumTest(EnumDay 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;

    case NAVEED:
        System.out.println(day + " is best.");
        break;

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

public static void main(String[] args) {
    EnumTest firstDay = new EnumTest(EnumDay.MONDAY);
    firstDay.tellItLikeItIs();
    EnumTest thirdDay = new EnumTest(EnumDay.WEDNESDAY);
    thirdDay.tellItLikeItIs();
    EnumTest fifthDay = new EnumTest(EnumDay.FRIDAY);
    fifthDay.tellItLikeItIs();
    EnumTest sixthDay = new EnumTest(EnumDay.SATURDAY);
    sixthDay.tellItLikeItIs();
    EnumTest seventhDay = new EnumTest(EnumDay.SUNDAY);
    seventhDay.tellItLikeItIs();
    EnumTest eigththDay = new EnumTest(EnumDay.NAVEED);
    eigththDay.tellItLikeItIs();
}
}
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
0

A java enum is similar to a class but is primarily used for constants. It uses a constructor, just like a class. I think a good way to understand how enums work is to see an example. Here's a little example I wrote myself a while back when I was learning.

    public class UseEnum {  
public enum Colors {
    RED("red", 1), BLUE("blue", 2), GREEN("green", 3), 
    YELLOW("yellow", 4), ORANGE("orange", 5), PURPLE("purple", 6), 
    PINK("pink", 7), WHITE("white", 8), BLACK("black", 9);
    private String color = "";
    private int id;
    private Colors(String color, int id) {
        this.color = color;
        this.id = id;
    }       
    private String getColor() {
        return color;
    }       
    private int getId() {
        return id;
    }
}
public static void main(String[] args) {
    while(true){    //infinite loop
        UseEnum ue = new UseEnum();
        ue.findColor(option);
    }
}
private static int option;  
private UseEnum() {
    try{
        Scanner s = new Scanner(System.in);
        System.out.print("Enter an ID: ");
        option = s.nextInt();
    }catch(Exception e){
        System.out.println("\terror: invalid number");
        System.exit(1);
    }
}   
private void findColor(int id) {
    boolean found = false;
    if(id == 10)
        System.exit(0);
    for(Colors c : Colors.values()) {
        if(c.getId() == id) {
            System.out.println("\t"+c.getColor());
            found = true;
            break;
        }
    }
    if(found != true)
        System.out.println("\tinvalid");
}

}

Enums can be used in switch statements which makes them quite useful.

I recommend this article for further reading: javarevisited

Troubleshoot
  • 1,816
  • 1
  • 12
  • 19