top of page
shubhangisingh453

Understanding Enum in detail in Java



In Java, an enum is a special data type that allows us to define a set of predefined constants. The constants are treated as objects, and we can define methods and constructors for the enum type. In this section, we will discuss each and every topic related to Java enum Class in detail with examples.


📗 Defining an enum


To define an enum in Java, we use the enum keyword followed by the name of the enum. The constants are defined within curly braces and are separated by commas. Here is an example:


enum Day {
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

In this example, we define an enum called Day with seven constants representing the days of the week.


Enum constructors and methods


Enums can have constructors and methods just like classes. In this example, we define an enum called Day with seven constants representing the days of the week. Each constant has an associated abbreviation that is passed to the constructor. The getAbbreviation() method returns the abbreviation for the given constant. Here is an example:


enum Day {
  MONDAY("Mon"), TUESDAY("Tue"), WEDNESDAY("Wed"), THURSDAY("Thu"), FRIDAY("Fri"), SATURDAY("Sat"), SUNDAY("Sun");

  private final String abbreviation;

  private Day(String abbreviation) {
    this.abbreviation = abbreviation;
  }

  public String getAbbreviation() {
    return abbreviation;
  }
}

Enum values() and valueOf() methods


The values() method returns an array of all the constants in the enum. The valueOf() method returns the constant with the specified name. Here is an example:


enum Day {
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Day[] days = Day.values(); // returns an array of all the days
Day monday = Day.valueOf("MONDAY"); // returns the MONDAY constant

Enum ordinal() method


The ordinal() method returns the index of the constant in the enum. The first constant has an ordinal value of zero, and the subsequent constants have ordinal values that are one greater than the previous constant. Here is an example:


enum Day {
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

int mondayIndex = Day.MONDAY.ordinal(); // returns 0int tuesdayIndex = Day.TUESDAY.ordinal(); // returns 1

Enum switch statement


We can use enums in switch statements. Here is an example:


enum Day {
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Day day = Day.MONDAY;

switch (day) {
  case MONDAY:
    System.out.println("Today is Monday");
    break;
  case TUESDAY:
    System.out.println("Today is Tuesday");
    break;
  case WEDNESDAY:
    System.out.println("Today is Wednesday");
    break;
  case THURSDAY:
    System.out.println("Today is Thursday");
    break;
  case FRIDAY:
    System.out.println("Today is Friday");
    break;
  case SATURDAY:
    System.out.println("Today is Saturday");
    break;
  case SUNDAY:
    System.out.println("Today is Sunday");
    break;
}

Enum constants with fields and methods


We can define fields and methods for each enum constant. Here is an example:


enum Day {
  MONDAY("Mon", 1), TUESDAY("Tue", 2), WEDNESDAY("Wed", 3), THURSDAY("Thu", 4), FRIDAY("Fri", 5), SATURDAY("Sat", 6), SUNDAY("Sun", 7);

  private final String abbreviation;
  private final int value;

  private Day(String abbreviation, int value) {
    this.abbreviation = abbreviation;
    this.value = value;
  }

  public String getAbbreviation() {
    return abbreviation;
  }

  public int getValue() {
    return value;
  }
}

In this example, we define an enum called Day with seven constants representing the days of the week. Each constant has an associated abbreviation and value that are passed to the constructor. The getAbbreviation() and getValue() methods return the abbreviation and value for the given constant.


Enum constructors with arguments


In some programming languages such as Java and C#, you can define an enumerated type with constructors that take arguments. This allows you to create instances of the enum with specific values, similar to how you would create instances of a class.

Here is an example of how you can define an enum with constructors that take arguments in Java:


public enum Size {
   SMALL(10),
   MEDIUM(20),
   LARGE(30);

   private int value;

   private Size(int value) {
      this.value = value;
   }

   public int getValue() {
      return value;
   }
}

In this example, the Size enum has three values: SMALL, MEDIUM, and LARGE. Each of these values is created with an integer argument that is used to set the value field of the enum.

You can access the value of each enum instance by calling the getValue() method. For example, Size.SMALL.getValue() would return 10.


Enum string


In Java, an enumerated type (enum) can also contain string values. Here's an example of how you can define an enum with string values:


public enum Color {
   RED("FF0000"),
   GREEN("00FF00"),
   BLUE("0000FF");

   private String hexCode;

   private Color(String hexCode) {
      this.hexCode = hexCode;
   }

   public String getHexCode() {
      return hexCode;
   }
}

In this example, the Color enum has three values: RED, GREEN, and BLUE. Each of these values is created with a string argument that is used to set the hexCode field of the enum.

You can access the value of each enum instance by calling the getHexCode() method. For example, Color.RED.getHexCode() would return "FF0000".

Enums with string values can be useful when you want to associate a string with each enum value. They can also be used to represent a fixed set of string values in your code, which can make your code more readable and less error-prone.


Thanks for reading, and happy coding!


Understanding Enum in detail in Java -> Understanding Reflection in Java: A Guide to Dynamic Code Analysis and Modification


bottom of page