0

Now this bothers me. Why do I need to create an enum than creating a normal java class? what extra advantage it gives me? I really do not understand enums. Now I saw this code in stackoverflaw itself.

enum Cats {
  FELIX(2), SHEEBA(3), RUFUS(7);

  private int mAge;
  Cats(int age) {
    mAge = age;
  }
  public int getAge() {
    return mAge;
   }
}   

And I wonder why it is an enum than a class... please explain this to me. Thanks in advance. update:

I need this for a airplane management in a airport. It was hard to figure out where to put an enum.And that's it.

Abbie
  • 41
  • 4
  • There are some discussions [here](http://stackoverflow.com/questions/15734225/what-are-the-differences-between-a-java-enum-and-a-class-with-private-constructo) and [here](http://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel) which talk about the differences between classes and enums, and when to use each. – azurefrog May 03 '14 at 04:53
  • 1
    [This is a really good read on the subject](http://javarevisited.blogspot.no/2011/08/enum-in-java-example-tutorial.html). The entire thing is well worth a read, so better go there than I summarize it. –  May 03 '14 at 04:57
  • ok one more thing can we use enum in creating a airplane management(according to time, queues etc) in a airport? . – Abbie May 03 '14 at 05:01
  • Depends on what your needs are. If you can think of at least one thing which you know will take on one of a set of well-defined constant values, then enums might work for you (e.g. the airplane models that an airline possesses) – awksp May 03 '14 at 05:02
  • 1
    @pewpew I think I am getting it there. – Abbie May 03 '14 at 05:03
  • @user3580294 Yes I need to initialize the planes in names and the arriving and departure time and destinations so I can use it right. – Abbie May 03 '14 at 05:04
  • Are any of those things well-defined constants that you would know completely at compile time? – awksp May 03 '14 at 05:07
  • Let's say an airplane can only have seats, trays and baggages and NOTHING ELSE. Then you can use an enum called "AirplaneFilling" that only consists of Seat, Tray and Baggage. If the plane can have any thing you can think of, from people to any specific candybar to another airplane inside it then you should not use enums, because (as @user3580294 is saying) these are not well-defined constants. –  May 03 '14 at 05:08

1 Answers1

1

enum looks like a special class,it's a class actually. They are all extend java.lang.Enum<E> . When we compiled the enum, it will be a Cats.class , all the enum values will be " public static final". Like this:

public enum Season {
    SPRING, SUMMER, AUTUMN, WINTER;
}

will likes(not equal):

public class Season {
    public static final int SPRING = 1;
    public static final int SUMMER = 2;
    public static final int AUTUMN = 3;
    public static final int WINTER = 4;
}

The diffrences is: Type safety and readability. You can see this post:What's the use of "enum" in Java?. for using enum

Community
  • 1
  • 1
从此醉
  • 44
  • 6