Java Enumeration Tutorial – Enum In Java With Examples

 Java Enumeration Tutorial – Enum In Java With Examples from Coding compiler Java tutorials. Here you will learn about Java enumeration type variable, enumerate switch and enumeration comparisons. Learn Now.!

Java Tutorial – Java Enumeration

A Java enumeration is a list of named constants.

Java Enumeration Explanation

Create an enumeration using the enum keyword.

For example, here is a simple enumeration:

enum Direction {
  East, South, West, North
}

The identifier East, South is called an enumeration constant. Enumeration constants are implicitly declared as public static final members of Direction.

Java Enumeration type variable

Once the enumeration is defined, you can create variables of that type.

Even if the enumeration defines a class type, you don’t use new to instantiate an enumeration.

The following code declares ap as the variable Direction of the enumerated type:

Direction ap;

Since ap is a type of Direction, the only values it can be assigned are those defined by the enumeration. For example, this assigns an ap value of South:

ap = Direction.South;

Comparison

You can compare the equality operators of two enumeration constants by using the == relationship.

// An enumeration of direction varieties. 
enum Direction {
  East, South, West, North
}

public class Main {
  public static void main(String args[]) {
    Direction dir = Direction.South;

    System.out.println("Value of dir: " + dir);

    dir = Direction.South;

    if (dir == Direction.South){
      System.out.println("ap contains GoldenDel.\n");
    }
  }
}

Enumerate Switch

Enumeration values can also be used to control switch statements.

enum Direction {
  East, South, West, North
}

public class Main {
  public static void main(String args[]) {
    Direction dir = Direction.South;
    switch (dir) {
    case South:
      System.out.println("south");
      break;
    case East:
      System.out.println("East");
      break;
    case West:
      System.out.println("West");
      break;
    case North:
      System.out.println("North.");
      break;
    }
  }
}

Java sheet number () / valueOf () method

All enumerations automatically include two predefined methods: values()and valueOf().

Their general form is:

public static enum-type[ ] values()
public static enum-type valueOf(String str)

values()The method returns an array containing a list of enumeration constants. valueOf()The method returns an enum constant whose value corresponds to the string passed in str.

The following program demonstrates values()and valueOf()methods:

enum Direction {
  East, South, West, North
}

public class Main {
  public static void main(String args[]) {
    Direction all[] = Direction.values();
    for (Direction a : all){
      System.out.println(a);
    }
    System.out.println();

    Direction dir = Direction.valueOf("South");
    System.out.println(dir);
  }
}

Compare enumerations

The following code shows how to compare enumerated data types.

enum Week {
  Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

public class Main {
  public static void main(String args[]) {

    Week day1, day2, day3;
    day1 = Week.Monday;
    day2 = Week.Tuesday;
    day3 = Week.Friday;

    // 
    if (day1.compareTo(day2) < 0)
      System.out.println(day1 + " comes before " + day2);

    if (day2.compareTo(day3) > 0)
      System.out.println(day2 + " comes before " + day3);

    if (day1.compareTo(day3) == 0)
      System.out.println(day1 + " equals " + day3);

  }
}

Related Java Tutorials & Interview Questions

Related Java Tutorials & Interview Questions
Introduction to Java Programming Java Annotations Tutorial For Beginners
Java Keywords Tutorial For Beginners Java Generics Tutorial For Beginners
Java Data Types Tutorial For Beginners Core Java Multiple Choice Questions
Java Operators Tutorial For Beginners 21 Aricent Java Interview Questions
Java Control Flow Statements Tutorial 53 Accenture Java Interview Questions
Java Performance Tuning Tips 399 Core Java Interview Questions
Java Class Tutorial For Beginners 581 Advanced Java Interview Questions
Java Exceptions Tutorial For Beginners 60 Java Multiple Choice Questions And Answers

Leave a Comment