New Java Topics: Enums

The J2SE version 5.0 of the Java programming language added enumerated types whose declaration syntax is similar to that of C:

  enum Cardsuit { CLUBS, DIAMONDS, SPADES, HEARTS };
  ...
  Cardsuit trump;

This lesson is taken from Wikipedia.

The Java type system treats enumerations as a type separate from integers, and intermixing of enum and integer values is not allowed. In fact, an enum type in Java is actually a special compiler-generated class rather than an arithmetic type, and enum values behave as global pre-generated instances of that class. Enum types can have instance methods and a constructor (the arguments of which can be specified separately for each enum value). All enum types implicitly extend the
Enum abstract class. An enum type cannot be instantiated directly.

Internally, each enum value contains an integer, corresponding to the order in which they are declared in the source code, starting from 0. The programmer cannot set a custom integer for an enum value directly, but one can define overloaded constructors that can then assign arbitrary values to self-defined members of the enum class. Defining getters allows then access to those self-defined members. The internal integer can be obtained from an enum value using the ordinal() method, and the list of enum values of an enumeration type can be obtained in order using the values() method. It is generally discouraged for programmers to convert enums to integers and vice versa. Enumerated types are Comparable, using the internal integer; as a result, they can be sorted.



Basic Enum Features in Java



Advanced Enum Features in Java

The following video describes some of the more advanced enum features in Java. Also described in this video are two library utility classes that are used with Java enums.  The EnumSet class implements a Set of enum values. The EnumMap class implements a Map of enum values to object.



Review of Key Enum Features


Beginner’s Guide to Java eNum – Why and for What should I use Enum? Java Enum Examples

beginners-guide-to-java-enum-why-and-for-what-should-i-use-enum

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. For instance, in a chess game you could represent the different types of pieces as an enum:

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants(contract status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”).

If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.

Java Example:

Output:

Some important points on Java Enum:

Point-1

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

Point-2

Enum in Java are type-safe: Enum has there own name-space. It means your enum will have a type for example “Company” in below example and you can not assign any value other than specified in Enum Constants.

Point-3

You can specify values of enum constants at the creation time. MyEnum.values() returns an array of MyEnum’s values.

Output:

Point-4

Enum constants are implicitly static and final and can not be changed once created.

Point-5

Enum can be safely compare using:

  1. Switch-Case Statement
  2. == Operator
  3. .equals() method (Note that == and .equals() return the same result when comparing to Enums of the same Enum class)

Please follow complete tutorial.

Point-6

You can not create instance of enums by using new operator in Java because constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself.

Point-7

Instance of Enum in Java is created when any Enum constants are first called or referenced in code.

Point-8

An enum specifies a list of constant values assigned to a type.

Point-9

An enum can be declared outside or inside a class, but NOT in a method.

Point-10

An enum declared outside a class must NOT be marked static, final , abstract, protected , or private

Point-11

Enums can contain constructors (but only private constructors), methods, variables, and constant class bodies.

Point-12

enum constants can send arguments to the enum constructor, using the syntax BIG(8), where the int literal 8 is passed to the enum constructor.

Point-13

enum constructors can have arguments, and can be overloaded.

Point-14

enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.

Point-15

The semicolon at the end of an enum declaration is optional.

These are legal:

  • enum Foo { ONE, TWO, THREE}
  • enum Foo { ONE, TWO, THREE};