{"id":593,"date":"2014-12-19T07:16:12","date_gmt":"2014-12-19T07:16:12","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=593"},"modified":"2014-12-19T08:52:42","modified_gmt":"2014-12-19T08:52:42","slug":"working-with-enum-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/working-with-enum-java\/","title":{"rendered":"Working with ENUM Java"},"content":{"rendered":"<p><strong>Working with ENUM<\/strong><br \/>\nENUM is just a list of set of value which are static and final.Executing the below code will have a output like one below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Test {\r\n\tpublic enum Company {\r\n\t\tEBAY, PAYPAL, GOOGLE, YAHOO, ATT\r\n\t}\r\n\r\n\tpublic static void main(String&#x5B;] args) {\r\n                System.out.println(Company.EBAY);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  EBAY\r\n<\/pre>\n<p>\nNow what you can do is to make this constant (static and final) to have further attributes like one below.\n<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic enum Company {\r\nEBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25);\r\nprivate int value;\r\n \r\nprivate Company(int value) {\r\nthis.value = value;\r\n}\r\n}\r\n<\/pre>\n<p>and more Values with overloaded constructor<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Test1 {\r\n\r\n\tpublic enum JobStatus {\r\n\t\tOPEN(&quot;Open&quot;, &quot;Its Open&quot;, &quot;1&quot;), ERROR(&quot;Error&quot;, &quot;Its a Error&quot;, &quot;2&quot;), WORKING(\r\n\t\t\t\t&quot;Working&quot;, &quot;Its Working&quot;, &quot;3&quot;), CLOSED(&quot;Closed&quot;, &quot;Its Closed&quot;);\r\n\t\tprivate String\tvalue;\r\n\t\tprivate String\tlabel;\r\n\t\tprivate String\torder;\r\n\r\n\t\tprivate JobStatus(String label, String value, String order) {\r\n\t\t\tthis.label = label;\r\n\t\t\tthis.value = value;\r\n\t\t\tthis.order = order;\r\n\t\t}\r\n\r\n\t\tprivate JobStatus(String label, String value) {\r\n\t\t\tthis.label = label;\r\n\t\t\tthis.value = value;\r\n\t\t}\r\n\r\n\t\tpublic String getValue() {\r\n\t\t\treturn this.value;\r\n\t\t}\r\n\r\n\t\tpublic String getLabel() {\r\n\t\t\treturn this.label;\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tSystem.out.println(JobStatus.OPEN.value);\r\n\t\tSystem.out.println(JobStatus.OPEN.label);\r\n\t\tSystem.out.println(JobStatus.OPEN.order);\r\n\t\tSystem.out.println(JobStatus.CLOSED.order);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nIts Open\r\nOpen\r\n1\r\nnull\r\n<\/pre>\n<p>\nWhen you need a predefined list of values which do not represent some kind of numeric or textual data, you should use an enum. For instance, in a chess game you could represent the different types of pieces as an enum\n<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nenum ChessPiece {\r\nPAWN,\r\nROOK,\r\nKNIGHT,\r\nBISHOP,\r\nQUEEN,\r\nKING;\r\n}\r\n<\/pre>\n<p><strong>Assigning Values to ENUM<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic enum Company {\r\nEBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25);\r\nprivate int value;\r\n \r\nprivate Company(int value) {\r\nthis.value = value;\r\n}\r\n}\r\n<\/pre>\n<ul>\n<li>All enums implicitly extend java.lang.Enum.<\/li>\n<li>MyEnum.values() returns an array of MyEnum\u2019s values.<\/li>\n<li>Enum constants are implicitly static and final and can not be changed once created.<\/li>\n<li>Enum can be safely compare using \u201c==\u201d equality operator<\/li>\n<li>An enum can be declared outside or inside a class, but NOT in a method.<\/li>\n<li>An enum declared outside a class must NOT be marked static, final , abstract, protected , or private<\/li>\n<li>Enums can contain constructors, methods, variables, and constant class bodies.<\/li>\n<li>enum constructors can have arguments, and can be overloaded.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Working with ENUM ENUM is just a list of set of value which are static and final.Executing the below code will have a output like one below public class Test { public enum Company { EBAY, PAYPAL, GOOGLE, YAHOO, ATT } public static void main(String&#x5B;] args) { System.out.println(Company.EBAY); } } Output EBAY Now what you&hellip; <a href=\"https:\/\/codethataint.com\/blog\/working-with-enum-java\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85],"tags":[],"class_list":["post-593","post","type-post","status-publish","format-standard","hentry","category-enum"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=593"}],"version-history":[{"count":7,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/593\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}