-3

I am not able to understand enum when i encountered Enum<Type>.Generally, we use enums as a separate class or as a class member.

a)What are the reasons and benefits of declaring an enum as a separate class or as a class member ? Can anyone explain ?

b)When would one extend Enum<Type> instead of using enum ? What are the benefits of extending Enum<Type>.

I happened to bump into the question which addresses valueOf() method in enum and Enum<Type> but that is very generic.

I went through the Oracle documentation but that doesn't explain the power of enums, atleast i didn't find the information i was looking for.

If someone can point me to an example or documentation which explains enums at the grass root level ,it would be great.

EDIT: Made questions more clearer. Changed the topic to be more relevant.

Community
  • 1
  • 1
Adithya
  • 2,923
  • 5
  • 33
  • 47
  • 1
    The documentation you've linked to *is* arguably the documentation to explain enums at grass root level. What information about "the power of enums" do you feel is missing? – Andrzej Doyle Jun 19 '13 at 10:37
  • 2
    1. In response to your title: no. 2. In response to your question: incomprehensible. – user207421 Jun 19 '13 at 10:56
  • I have made questions more clear and comprehensible. Apologies for the inconvenience. – Adithya Jun 19 '13 at 11:33

3 Answers3

2

enum is a very powerful if we know how to use enum and when to use enum

Case 1: When you have string contant values and you are comparing them.

How enum helps: Crate enum of all strings and instread of comparing string compare emum. As enum compare on the basis of their hash value which is a number so it is fast then the string comparision.

As shown in the Oracle documentation they used Sunday, Monday as enum not String.

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

Case 2: When you have to constants and you have to compare constants because value of some other constant is based on result. for eg. You have months name and on the basis of month names you get how many days are there and start day type in this case enum helps you a lot.

for eg.

enum MonthEnum {
    JAN("Jan", 31, "Sunday"), Feb("Feb", 28, "Tuesday"), March("March", 28, "Monday");

    private final String monthName;
    private final int noOfDays;
    private final String startDay;

    private MonthEnum(final String monthName, final int noOfDays, final String startDay) {
        this.monthName = monthName;
        this.noOfDays = noOfDays;
        this.startDay = startDay;
    }

    public String getMonthName() {
        return monthName;
    }

    public int getNoOfDays() {
        return noOfDays;
    }

    public String getStartDay() {
        return startDay;
    }
}

and you can easily get enum by passing only monthName

public static MonthEnum getMonthEnum(final String monthName) {
        MonthEnum month = null;
        for(MonthEnum monthEnum : MonthEnum.values()) {
            if(monthEnum.getMonthName().equalsIgnoreCase(monthName)) {
                month = monthEnum;
            }
        }
        return month;
    }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
2

enum isn't a primative - it's a keyword which is used for declaring an enum, a sort of syntactic sugar if you will.

You would always define an enumeration using the enum keyword, rather than extending the Enum<T> abstract class yourself, because there is no benefit to doing the latter. An enumeration defined with enum is compiled into a class extending Enum by the compiler, which also ensures that appropriate implementations of methods are autogenerated.

So the Enum class exists because it has to, but on the whole it's just the internals of what makes enums work, and you can more or less ignore it. It's entirely reasonable to treat enums as enums, and ignore the exact bytecode that the compiler will generate.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
1

enum is not a primitive type.

From Oracle Docs:

An enum type is a special data type that enables for a variable to be a set of predefined constants.

Moreover, the enum tutorials explains enum well at basic level. Pasting the extract below:

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum 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. For example, this code from the Planet class example below iterates over all the planets in the solar system.

for (Planet p : Planet.values()) {
    System.out.printf("Your weight on %s is %f%n",
                      p, p.surfaceWeight(mass)); }

Note: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Nargis
  • 4,687
  • 1
  • 28
  • 45