-1

I am getting confused regarding Enum.

Where and how to use Enum?

If I will use Enum instead of constant then what could be the benefit?

Could someone please explain to me?

Mind Peace
  • 905
  • 8
  • 29
Surya
  • 63
  • 1
  • 2
  • 6
  • 4
    What is "constant class"? – gexicide Jul 17 '14 at 19:48
  • public ApplicationConstant{ private static final String male="MALE"; } – Surya Jul 17 '14 at 19:55
  • possible duplicate of [What's the advantage of a Java enum versus a class with public static final fields?](http://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel) or [this](Static String constants VS enum in Java 5+) or [this](https://stackoverflow.com/questions/2229297/java-enumerations-vs-static-constants). – awksp Jul 17 '14 at 22:19

3 Answers3

1

The primary advantage is type safety. With a set of constants, any value of the same intrinsic type could be used, introducing errors. With an enum only the applicable values can be used.

Mind Peace
  • 905
  • 8
  • 29
1

An Enum class is used to specify a range of constants that will be frequently used within an application.

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

You should use enum types any time you need to represent a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

A constant class (assuming this is what you are talking about), is really specified for one solid object that you wouldn't change. Technically, an Enum class is a constant class with more definitions, but an advantage to an Enum class is some pre-defined functions that you would have to define in your own constant class. However, you should also note that this may be overkill for small, singleton examples.

Additionally, Enum classes are type-safe, whereas static fields aren't. Compile time error checking is now possible, versus the run-time potential errors that will occur with a constant class. This furthermore improves readability, because instead of having errors where an index of a list of constants is unavailable. This is all well explained in this post, by the current best answer.

Community
  • 1
  • 1
ihgann
  • 505
  • 3
  • 11
0

As of Java 8.0 the keyword "constant" does not exist.

If you mean "const": This keyword is in the known keywords in Java (from the beginning until today - 8.0), but it has no use and any compiler shouldn't compile if it find's this keyword.

Enumerations: Use them when you have specific logical values and don't want to handle them in String, like:

public enum AcceptableColours {
    BLACK,
    WHITE,
    GREY
}

It's much safer during development time to match enum values

AcceptableColours currentColour = object.getColour;
if (currentColour == acceptableColours.BLACK) {
    // do something
} else if (currentColour == acceptableColours.WHITE) {
    // do something different
}

than to match string values

AcceptableColours currentColor = object.getColour;
if (currentColor.equals("black")) {
    // do something
} else if (currentColor.equals("white")) {
    // do something different
}

... imagine huge applications with much more variations on enum values (like banking software, insurance software, ...), where it's much easier to misspell some string values, and voilà, happy debugging...

Ronald Duck
  • 323
  • 1
  • 11