{"id":1235,"date":"2022-02-26T06:20:13","date_gmt":"2022-02-26T06:20:13","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1235"},"modified":"2022-02-26T06:20:13","modified_gmt":"2022-02-26T06:20:13","slug":"java-enums","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/java-enums\/","title":{"rendered":"Java enums"},"content":{"rendered":"\n<p>In this tutorial, we will learn about enums in Java. We will learn to create and use enums and enum classes with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">In Java, an enum (short for enumeration) is a type that has a fixed set of constant values. We use the&nbsp;<code>enum<\/code>&nbsp;keyword to declare enums. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size { \n   SMALL, MEDIUM, LARGE, EXTRALARGE \n}<\/code><\/pre>\n\n\n\n<p>Here, we have created an enum named&nbsp;<var>Size<\/var>. It contains fixed values&nbsp;<var>SMALL<\/var>,&nbsp;<var>MEDIUM<\/var>,&nbsp;<var>LARGE<\/var>, and&nbsp;<var>EXTRALARGE<\/var>.<\/p>\n\n\n\n<p>These values inside the braces are called enum constants (values).<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The enum constants are usually represented in uppercase.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example 1: Java Enum<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size {\n   SMALL, MEDIUM, LARGE, EXTRALARGE\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      System.out.println(Size.SMALL);\n      System.out.println(Size.MEDIUM);\n   }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>SMALL\nMEDIUM<\/samp><\/pre>\n\n\n\n<p>As we can see from the above example, we use the enum name to access the constant values.<\/p>\n\n\n\n<p>Also, we can create variables of enum types. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size pizzaSize;<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>pizzaSize<\/var>&nbsp;is a variable of the&nbsp;<var>Size<\/var>&nbsp;type. It can only be assigned with 4 values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pizzaSize = Size.SMALL;\npizzaSize = Size.MEDIUM;\npizzaSize = Size.LARGE;\npizzaSize = Size.EXTRALARGE;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-with-switch\">Example 2: Java Enum with the switch statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size {\n SMALL, MEDIUM, LARGE, EXTRALARGE\n}\n\nclass Test {\n Size pizzaSize;\n public Test(Size pizzaSize) {\n   this.pizzaSize = pizzaSize;\n }\n public void orderPizza() {\n   switch(pizzaSize) {\n     case SMALL:\n       System.out.println(\"I ordered a small size pizza.\");\n       break;\n     case MEDIUM:\n       System.out.println(\"I ordered a medium size pizza.\");\n       break;\n     default:\n       System.out.println(\"I don't know which one to order.\");\n       break;\n   }\n }\n}\n\nclass Main {\n public static void main(String&#91;] args) {\n   Test t1 = new Test(Size.MEDIUM);\n   t1.orderPizza();\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>I ordered a medium size pizza.<\/samp><\/pre>\n\n\n\n<p>In the above program, we have created an enum type&nbsp;<var>Size<\/var>. We then declared a variable&nbsp;<var>pizzaSize<\/var>&nbsp;of the&nbsp;<var>Size<\/var>&nbsp;type.<\/p>\n\n\n\n<p>Here, the variable&nbsp;<var>pizzaSize<\/var>&nbsp;can only be assigned with 4 values (<var>SMALL<\/var>,&nbsp;<var>MEDIUM<\/var>,&nbsp;<var>LARGE<\/var>,&nbsp;<var>EXTRALARGE<\/var>).<\/p>\n\n\n\n<p>Notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Test t1 = new Test(Size.MEDIUM);<\/code><\/pre>\n\n\n\n<p>It will call the&nbsp;<code>Test()<\/code>&nbsp;constructor inside the&nbsp;<var>Test<\/var>&nbsp;class. Now, the variable&nbsp;<var>pizzaSize<\/var>&nbsp;is assigned with the&nbsp;<var>MEDIUM<\/var>&nbsp;constant.<\/p>\n\n\n\n<p>Based on the value, one of the cases of the&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/switch-statement\">switch case statement<\/a>&nbsp;is executed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"enum-class\">Enum Class in Java<\/h2>\n\n\n\n<p>In Java, enum types are considered to be a special type of class. It was introduced with the release of Java 5.<\/p>\n\n\n\n<p>An enum class can include methods and fields just like regular classes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size {\n    constant1, constant2, \u2026, constantN;\n\n    \/\/ methods and fields\t\n}<\/code><\/pre>\n\n\n\n<p>When we create an enum class, the compiler will create instances (objects) of each enum constants. Also, all enum constant is always&nbsp;<code>public static final<\/code>&nbsp;by default.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"enum-class-example\">Example 3: Java Enum Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size{\n  SMALL, MEDIUM, LARGE, EXTRALARGE;\n\n  public String getSize() {\n\n    \/\/ this will refer to the object SMALL\n    switch(this) {\n      case SMALL:\n        return \"small\";\n\n      case MEDIUM:\n        return \"medium\";\n\n      case LARGE:\n        return \"large\";\n\n      case EXTRALARGE:\n        return \"extra large\";\n\n      default:\n        return null;\n      }\n   }\n\n  public static void main(String&#91;] args) {\n\n    \/\/ call getSize()\n    \/\/ using the object SMALL\n    System.out.println(\"The size of the pizza is \" + Size.SMALL.getSize());\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>The size of the pizza is small<\/samp><\/pre>\n\n\n\n<p>In the above example, we have created an enum class&nbsp;<var>Size<\/var>. It has four constants&nbsp;<var>SMALL<\/var>,&nbsp;<var>MEDIUM<\/var>,&nbsp;<var>LARGE<\/var>&nbsp;and&nbsp;<var>EXTRALARGE<\/var>.<\/p>\n\n\n\n<p>Since&nbsp;<var>Size<\/var>&nbsp;is an enum class, the compiler automatically creates instances for each enum constants.<\/p>\n\n\n\n<p>Here inside the&nbsp;<code>main()<\/code>&nbsp;method, we have used the instance&nbsp;<var>SMALL<\/var>&nbsp;to call the&nbsp;<code>getSize()<\/code>&nbsp;method.<\/p>\n\n\n\n<p><strong>Note<\/strong>: Like regular classes, an enum class also may include constructors. To learn more about it, visit&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/enum-constructor\">Java enum constructor<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"enum-methods\">Methods of Java Enum Class<\/h2>\n\n\n\n<p>There are some predefined methods in enum classes that are readily available for use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Java Enum ordinal()<\/h3>\n\n\n\n<p>The&nbsp;<code>ordinal()<\/code>&nbsp;method returns the position of an enum constant. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ordinal(SMALL) \n\/\/ returns 0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Enum compareTo()<\/h3>\n\n\n\n<p>The&nbsp;<code>compareTo()<\/code>&nbsp;method compares the enum constants based on their ordinal value. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size.SMALL.compareTo(Size.MEDIUM)\n \/\/ returns ordinal(SMALL) - ordinal(MEDIUM)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Enum toString()<\/h3>\n\n\n\n<p>The&nbsp;<code>toString()<\/code>&nbsp;method returns the string representation of the enum constants. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SMALL.toString()\n\/\/ returns \"SMALL\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Enum name()<\/h3>\n\n\n\n<p>The&nbsp;<code>name()<\/code>&nbsp;method returns the defined name of an enum constant in string form. The returned value from the&nbsp;<code>name()<\/code>&nbsp;method is&nbsp;<code>final<\/code>. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name(SMALL)\n\/\/ returns \"SMALL\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Java Enum valueOf()<\/h3>\n\n\n\n<p>The&nbsp;<code>valueOf()<\/code>&nbsp;method takes a string and returns an enum constant having the same string name. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size.valueOf(\"SMALL\")\n\/\/ returns constant SMALL.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Enum values()<\/h3>\n\n\n\n<p>The&nbsp;<code>values()<\/code>&nbsp;method returns an array of enum type containing all the enum constants. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size&#91;] enumArray = Size.value();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why\">Why Java Enums?<\/h2>\n\n\n\n<p>In Java, enum was introduced to&nbsp;<strong>replace the use of int constants<\/strong>.<\/p>\n\n\n\n<p>Suppose we have used a collection of&nbsp;<code>int<\/code>&nbsp;constants.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Size {\n   public final static int SMALL = 1;\n   public final static int MEDIUM = 2;\n   public final static int LARGE = 3;\n   public final static int EXTRALARGE = 4;\n}<\/code><\/pre>\n\n\n\n<p>Here, the problem arises if we print the constants. It is because only the number is printed which might not be helpful.<\/p>\n\n\n\n<p>So, instead of using int constants, we can simply use enums. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size {\n   SMALL, MEDIUM, LARGE, EXTRALARGE\n}<\/code><\/pre>\n\n\n\n<p>This makes our code more intuitive.<\/p>\n\n\n\n<p>Also, enum provides&nbsp;<strong>compile-time type safety<\/strong>.<\/p>\n\n\n\n<p>If we declare a variable of the&nbsp;<var>Size<\/var>&nbsp;type. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size size;<\/code><\/pre>\n\n\n\n<p>Here, it is guaranteed that the variable will hold one of the four values. Now, If we try to pass values other than those four values, the compiler will generate an error.<a href=\"https:\/\/www.programiz.com\/java-programming\/singleton\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about enums in Java. We will learn to create and use enums and enum classes with the help of examples. In Java, an enum (short for enumeration) is a type that has a fixed set of constant values. We use the&nbsp;enum&nbsp;keyword to declare enums. For example, Here, we have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[556],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1235"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1235"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1235\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}