{"id":1437,"date":"2022-03-04T05:41:17","date_gmt":"2022-03-04T05:41:17","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1437"},"modified":"2022-03-04T05:41:17","modified_gmt":"2022-03-04T05:41:17","slug":"create-an-enum-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/04\/create-an-enum-class\/","title":{"rendered":"Create an enum class"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Java program to create an enum class<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size{\n\n  \/\/ enum constants\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 the method getSize()\n     \/\/ using the object SMALL\n     System.out.println(\"The size of Pizza I get is \" + Size.SMALL.getSize());\n\n     \/\/ call the method getSize()\n     \/\/ using the object LARGE\n     System.out.println(\"The size of Pizza I want is \" + Size.LARGE.getSize());\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The size of Pizza I get is small\nThe size of Pizza I want is large<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an enum class named&nbsp;<var>Size<\/var>. The class contains four constants&nbsp;<var>SMALL<\/var>,&nbsp;<var>MEDIUM<\/var>,&nbsp;<var>LARGE<\/var>, and&nbsp;<var>EXTRALARGE<\/var>.<\/p>\n\n\n\n<p>Here, the compiler automatically converts all the constants of the enum into its instances. Hence, we can call the method using the constant as objects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size.SMALL.getSize()<\/code><\/pre>\n\n\n\n<p>In this call, the&nbsp;<code>this<\/code>&nbsp;keyword is now associated with the&nbsp;<var>SMALL<\/var>&nbsp;object. Hence, the value small is returned.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java program to create an enum class Output In the above example, we have created an enum class named&nbsp;Size. The class contains four constants&nbsp;SMALL,&nbsp;MEDIUM,&nbsp;LARGE, and&nbsp;EXTRALARGE. Here, the compiler automatically converts all the constants of the enum into its instances. Hence, we can call the method using the constant as objects. In this call, the&nbsp;this&nbsp;keyword is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[558],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1437"}],"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=1437"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}