Enum of Strings Java

Enum of Strings Java | The enum is a keyword that is present in the special classes to represent constants. To create an enum we use the enum keyword and separate constants with a comma. The enum stands for enumeration which means “specifically listed”, these are more used in switch statements.

These enums are just like classes the difference is they are by default public, static, and final. They cannot create an object and cannot extend other classes. Usually, we use enum when we are dealing with months, colors, weeks, etc.
Example for enum:-

enum Levels {
   LOW, MEDIUM, HIGH
}

public class Main {
   public static void main(String[] args) {
      Levels myVar = Levels.MEDIUM;
      switch (myVar) {
      case LOW:
         System.out.println("Low level");
         break;
      case MEDIUM:
         System.out.println("Medium level");
         break;
      case HIGH:
         System.out.println("High level");
         break;
      }
   }
}

Output:-

Medium level

Java Enum Of Strings Example

enum Levels {
   LOW {
      public String toString() {
         return "The size is LOW.";
      }
   },
   SMALL {
      public String toString() {
         return "The size is small.";
      }
   },
   MEDIUM {
      public String toString() {
         return "The size is medium.";
      }
   };
}

public class Main {
   public static void main(String[] args) {
      System.out.println(Levels.MEDIUM.toString());
      System.out.println(Levels.SMALL.toString());
      System.out.println(Levels.LOW.toString());
   }
}

Output:

The size is medium.
The size is small.
The size is LOW.

Get String Of Enum Java

Here our goal is to access the enum of strings. Let us see another example for the same.

enum Levels {
   SMALL, MEDIUM, LARGE, EXTRALARGE
}

public class Main {
   public static void main(String[] args) {
      System.out.println("The string value of SMALL is " 
         + Levels.SMALL.toString());
      System.out.println("The string value of MEDIUM is " 
         + Levels.MEDIUM.name());
      System.out.println("The string value of LARGE is " 
         + Levels.LARGE.name());
      System.out.println("The string value of EXTRALARGE is " 
         + Levels.EXTRALARGE.name());
   }
}

Output:-

The string value of SMALL is SMALL
The string value of MEDIUM is MEDIUM
The string value of LARGE is LARGE
The string value of EXTRALARGE is EXTRALARGE

Let us see more examples:-

public enum Levels {
   SMALL("Small"), MEDIUM("Medium"), 
   LARGE("Large"), EXTRALARGE("Extra Large");
   
   private String abbreviation;
   
   private Levels(String abbreviation) {
      this.abbreviation = abbreviation;
   }

   public String getAbbreviation() {
      return abbreviation;
   }
}
public class Main {
   public static void main(String[] args) {
      System.out.println("The string value of SMALL: " 
         + Levels.SMALL.getAbbreviation());
      System.out.println("The string value of MEDIUM: " 
         + Levels.MEDIUM.getAbbreviation());
      System.out.println("The string value of LARGE: " 
         + Levels.LARGE.getAbbreviation());
      System.out.println("The string value of EXTRALARGE: " 
         + Levels.EXTRALARGE.getAbbreviation());
   }
}

Output:-

The string value of SMALL: Small
The string value of MEDIUM: Medium
The string value of LARGE: Large
The string value of EXTRALARGE: Extra Large

Check If String Is Part Of Enum Java

enum Levels {
  SMALL, MEDIUM, LARGE, EXTRALARGE
}

public class Main {
  public static boolean contains(String test) {
    for (Levels c : Levels.values()) {
      if (c.name().equals(test)) {
        return true;
      }
    }
    return false;
  }

  public static void main(String[] args) {
    System.out.println(contains("SMALL"));
    System.out.println(contains("BIG"));
  }
}

Output:-

true
false

Let us see more examples:-

public enum Levels {
   SMALL("Small"), MEDIUM("Medium"), 
   LARGE("Large"), EXTRALARGE("Extra Large");
   
   private String abbreviation;
   
   private Levels(String abbreviation) {
      this.abbreviation = abbreviation;
   }

   public String getAbbreviation() {
      return abbreviation;
   }
}
public class Main {
  public static boolean contains(String test) {
    for (Levels c : Levels.values()) {
      if (c.getAbbreviation().equals(test)) {
        return true;
      }
    }
    return false;
  }

  public static void main(String[] args) {
    System.out.println(contains("SMALL"));
    System.out.println(contains("Small"));
    System.out.println(contains("BIG"));
    System.out.println(contains("EXTRALARGE"));
    System.out.println(contains("Extra Large"));
  }
}

Output:-

false
true
false
false
true

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.