{"id":1237,"date":"2022-02-26T06:34:52","date_gmt":"2022-02-26T06:34:52","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1237"},"modified":"2022-02-26T06:34:52","modified_gmt":"2022-02-26T06:34:52","slug":"java-enum-constructor","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/java-enum-constructor\/","title":{"rendered":"Java enum Constructor"},"content":{"rendered":"\n<p>In this Java tutorial, you can learn about enum constructors with the help of a working example.<\/p>\n\n\n\n<p id=\"introduction\">Before you learn about enum constructors, make sure to know about\u00a0Java enums.<\/p>\n\n\n\n<p>In Java, an enum class may include a constructor like a regular class. These enum constructors are either<\/p>\n\n\n\n<ul><li><strong>private<\/strong>&nbsp;&#8211; accessible within the class<br>or<\/li><li><strong>package-private<\/strong>&nbsp;&#8211; accessible within the package<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1\">Example: enum Constructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size {\n\n   \/\/ enum constants calling the enum constructors \n   SMALL(\"The size is small.\"),\n   MEDIUM(\"The size is medium.\"),\n   LARGE(\"The size is large.\"),\n   EXTRALARGE(\"The size is extra large.\");\n\n   private final String pizzaSize;\n\n   \/\/ private enum constructor\n   private Size(String pizzaSize) {\n      this.pizzaSize = pizzaSize;\n   }\n\n   public String getSize() {\n      return pizzaSize;\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      Size size = Size.SMALL;\n      System.out.println(size.getSize());\n   }\n}\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 is small.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an enum&nbsp;<var>Size<\/var>. It includes a&nbsp;<code>private<\/code>&nbsp;enum constructor. The constructor takes a string value as a parameter and assigns value to the variable&nbsp;<var>pizzaSize<\/var>.<\/p>\n\n\n\n<p>Since the constructor is&nbsp;<code>private<\/code>, we cannot access it from outside the class. However, we can use enum constants to call the constructor.<\/p>\n\n\n\n<p>In the&nbsp;<var>Main<\/var>&nbsp;class, we assigned SMALL to an enum variable&nbsp;<var>size<\/var>. The constant&nbsp;<var>SMALL<\/var>&nbsp;then calls the constructor&nbsp;<var>Size<\/var>&nbsp;with string as an argument.<\/p>\n\n\n\n<p>Finally, we called&nbsp;<code>getSize()<\/code>&nbsp;using&nbsp;<var>size<\/var>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Java tutorial, you can learn about enum constructors with the help of a working example. Before you learn about enum constructors, make sure to know about\u00a0Java enums. In Java, an enum class may include a constructor like a regular class. These enum constructors are either private&nbsp;&#8211; accessible within the classor package-private&nbsp;&#8211; accessible within [&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\/1237"}],"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=1237"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1237\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}