Enums

What

A list of ordered named constants. The constants are of type of that enum type. Cannot instantiate enum type.

public enum Direction {
  EAST, SOUTH, WEST, NORTH // Generally all capitals
}

When to use

  • When you need a predefined list of values which do represent some kind of numeric or textual data, you should use 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.

    • constants or flags

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

Properties

  • an enum cannot extend anything else.

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

    • Direction east = Direction.EAST;

    • Direction east = 1; // Compile error

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

  • Enum can be safely compare using

    • Switch-Case Statement

    • == Operator

    • .equals() method

  • 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

  • Enums can contain constructors, methods, variables, and constant class bodies.

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

  • enum constructors can have arguments, and can be overloaded.

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

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

To use an enum:

  • Notice the type of aDirection is the enum type, Direction.

  • To use enum, use it like static/class field

Use of ordinals.

  • Starts list at 0, like arrays

  • Avoid using ordinals, unless certain that the enum list will not be added to. (see effective java)

toString() and getName()

Implementing an inteface

  • When defining method in block, must also define it as abstract singature of method outside of block

  • Do not need to have interface to define method in block

Enum methods

  • enum type is actually a class type

  • compiler creates class of this enum

    • can have fields, constructors and methods

    • the class extends java.lang.Enum which has toSting(), equals() etc

  • The power of this way, is to avoid using decision/control statements and use polymorphism to implement the logic based on the enum type without having to know about the enum type

    • Benefit: reduce code bloat in calling code

    • Benefit: only one place to change logic or add new types, instead of in the calling class

Singleton

Actually equivalent to;

Use instead of Booleans

  • Avoid magic booleans, like avoiding magic numbers

  • If argument can have two values (true/false or implied to be), this may not stay true, others can be added, best to use an enum

  • Can have enum methods that state which is true or false

Why is it better?

  • it is more concise,

  • provides the serialization machinery for free,

  • provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks

Last updated

Was this helpful?