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.

8
  • There are some discussions here and here which talk about the differences between classes and enums, and when to use each. Commented May 3, 2014 at 4:53
  • 1
    This is a really good read on the subject. The entire thing is well worth a read, so better go there than I summarize it. Commented May 3, 2014 at 4:57
  • ok one more thing can we use enum in creating a airplane management(according to time, queues etc) in a airport? . Commented May 3, 2014 at 5: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) Commented May 3, 2014 at 5:02
  • 1
    @pewpew I think I am getting it there. Commented May 3, 2014 at 5:03

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.