Introduction to enum

This lesson is taken from Wikipedia.

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

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

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). We will deal with enum constructors in the next, advanced section on enums.

All enum types implicitly extend the Enumabstract 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. 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

The following video describes some 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 Basic enum Features

This lesson has been rewritten from a lesson originally found on Crunchify.com

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 Basic Points on Java's enum

    Invalid HTML tag: attribute name extend&nbsp;< is not allowed
  • 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.

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

  • 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)

  • 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.

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

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

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

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

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

Both of these will compile:

    1. enum Foo { ONE, TWO, THREE}
    2. enum Foo { ONE, TWO, THREE};

Questions about enum


1 point
Will the following code compile?

enum Enums
{
    ONE, TWO, THREE, ONE, FOUR;
}
1 point
Will the following code compile?

class A
{
     
}
 
enum Alphabet extends A
{
    ABC, BCD, CDE, DEF;
}
1 point
Will the following code compile?

enum Levels
{
    private TOP,
     
    public MEDIUM,
     
    protected BOTTOM;
}
1 point
Will the following code compile?

enum Directions
{
    NORTH, SOUTH, WEST, EAST;
     
    private Directions()
    {
        System.out.println(1);
    }
}
 
public class MainClass
{
    public static void main(String[] args)
    {
        Directions d1 = new Directions();
    }
}
1 point
How would you access the enum, below, from another class?

class MyClass
{
    enum MyEnum
    {
        A;
    }
}
1 point
Will the following code compile?

enum Levels
{
    int value;
     
    TOP, MEDIUM, BOTTOM;
}