<!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG-spv-->{"id":488642,"date":"2017-03-29T02:00:00","date_gmt":"2017-03-29T00:00:00","guid":{"rendered":"http:\/\/nhub.news\/?p=488642"},"modified":"2017-03-29T17:16:58","modified_gmt":"2017-03-29T15:16:58","slug":"understanding-enumeration-in-java-developer-com","status":"publish","type":"post","link":"http:\/\/nhub.news\/fr\/2017\/03\/understanding-enumeration-in-java-developer-com\/","title":{"rendered":"Understanding Enumeration in Java &#8211; Developer.com"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>Learn how enumerations can be utilized in Java.<\/b> <br \/>Enumeration is a recent (from JDK5) inclusion into the family of Java APIs. It basically represents a list of named constants. Apart from Java, almost all other prominent programming languages have the feature of enumeration. Although Java has the final keyword to represent constants, enumeration was included as a convenience to meet many of the streamlined needs of the programmer. This article tries to provide the background information and show how enumerations can be utilized in Java. <br \/>Unlike the features of enumeration in other languages, the enumeration designated by the keyword enum in Java represents a special type of class. This inherent capability bolstered the principle of expansion in Java. Because it is a class type, it has a constructor, methods, and instance variables associated with it. <br \/>The values defined by the enum are constant. This means they are implicitly static and final. It is not possible to change the values after declaration. The enum is ideally used to represent a set of fixed named constants. For example, to enumerate a list of gender varieties, we may write: <br \/>The identifiers Male , Female , and Third are called enumeration constants. As stated earlier, they are by default public static and final members of the enum class. These constants declared are of self typed. We can declare a enum variable as <br \/>and can assign a value as <br \/>We also can use a comparison operator as such: <br \/>Or, use it with the switch statements, as in this example: <br \/>Observe that we have not used any qualifier, such as Gender. Male , with the case statement; instead, we used only the named constant Male. This is possible because, when we have declared gender with the switch , statement it implicitly recognizes the type of the enum constants. <br \/>We can specify custom values with enum constants at the time of declaration. In the following code, we have specified some values. But, to actualize this principle, we must declare a private member variable and a private constructor. <br \/>Because the Enum class implements the Comparable interface, the use of compareTo() is its inherent capability. Moreover, the Java compiler automatically creates a static method, called values() , which returns an array of named constants defined in the enum. There is another static method, called valueOf() , which takes enum class types and the named constant in the string format. This method returns the enumeration constant of the value that corresponds to the string passed in the parameter. We can use them in the following manner: <br \/>Although Java enumeration is of the class type , we do not use the new keyword to instantiate it. The association of a class as its principle design gives it some extraordinary capability. It can be used like any other normal classes in Java. We can declare a constructor, define member variables and methods, and even implement an interface. When we create an enumeration constant, an object of that type actually is created. When the constructor is invoked, the enumeration constants are initialized. This also means that each enumeration object has its own copy of its member variables defined by it. Therefore, it can be said that following two enumeration declarations are quite distinct. <br \/>An enum class can inherit from a super class or implement a super interface. Following is a perfectly valid declaration: <br \/>However, all enumeration classes automatically inherit java.lang. Enum. As a result, the methods declared in java.lang. Enum are all available to the enum delaration. <br \/>The java.lang. Enum does not have a public constructor. The method declared in this class is compareTo() due to the implementation of the Comparable interface, clone() , hashCode() , toString() , and equals() due to the inheritance of the Object class. Other member methods declared are as follows: <br \/>Output: <br \/>It is possible to define an abstract class within enum as follows: <br \/>Enumeration can be used in many ways. What you&rsquo;ve seen here is another versatile feature of Java. This capability can be worked up to use according to the need of the programmer. The idea is to treat enumeration like a special class that defines a list of named constants. So, without using several final static variables to declare a collection of constants, we can use enumeration. The goal is to provide readability, conceptual purity, and ease of use.<\/p>\n<div id=\"td_post_ranks\" class=\"td-post-comments\" style=\"vertical-align: middle;\">\n<div style=\"float: left;\">Similarity rank: 1<\/div>\n<\/div>\n<p><script>\njQuery(function() {\nvar mainContentMetaInfo = '.td-post-header .meta-info';\nvar tdPostRanks = '#td_post_ranks';\nif (jQuery(tdPostRanks).length) {\n    var tdPostRanksHtml = jQuery(tdPostRanks).get(0).outerHTML;\n    if (typeof tdPostRanksHtml != 'undefined') {\n        jQuery(tdPostRanks).remove();\n        jQuery(mainContentMetaInfo).append(tdPostRanksHtml);\n    }\n}\n});\n<\/script><span>\u00a9 Source: <a href=\"http:\/\/www.developer.com\/java\/data\/understanding-enumeration-in-java.html\" target=\"_blank\">http:\/\/www.developer.com\/java\/data\/understanding-enumeration-in-java.html<\/a><br \/>\nAll rights are reserved and belongs to a source media.<\/span><\/p>\n<script>jQuery(function(){jQuery(\"#td_post_ranks\").remove();});<\/script><script>jQuery(function(){jQuery(\".td-post-content\").find(\"p\").find(\"img\").hide();});<\/script>","protected":false},"excerpt":{"rendered":"<p>Learn how enumerations can be utilized in Java. Enumeration is a recent (from JDK5) inclusion into the family of Java APIs. It basically represents a list of named constants. Apart from Java, almost all other prominent programming languages have the feature of enumeration. Although Java has the final keyword to represent constants, enumeration was included [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":488641,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts\/488642"}],"collection":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/comments?post=488642"}],"version-history":[{"count":1,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts\/488642\/revisions"}],"predecessor-version":[{"id":488643,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts\/488642\/revisions\/488643"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/media\/488641"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/media?parent=488642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/categories?post=488642"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/tags?post=488642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}