{"id":7912,"date":"2014-06-12T09:13:16","date_gmt":"2014-06-12T09:13:16","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=7912"},"modified":"2020-09-01T07:22:26","modified_gmt":"2020-09-01T07:22:26","slug":"enumerated-data-types-java","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/enumerated-data-types-java\/","title":{"rendered":"Enumerated Data Types In Java"},"content":{"rendered":"<p>Enumerated data types (or enums for short) are newly introduced in Java 5.0. Enums will let us choose a value from a pre-defined list of values. Each value in this pre-defined list are called \u201cenums\u201d. Using enums we can restrict the values thus by reducing bugs in our code.<\/p>\n<p>Suppose, if we are developing payroll application, we need to have certain employee types. For example, permanent employee, contract employee, full time employee and part-time employee etc. In order to provide these employee types, we can create an enum with these pre-defined employee types as shown below:<br \/>\n<code><br \/>\nenum EmployeeType { PERMANENT, CONTRACT, FULLTIME, PARTTIME }<br \/>\n<\/code><br \/>\nOnce we have enum type in place, we can choose the required enum type as shown below.<br \/>\n<code><br \/>\nclass Employee {<br \/>\nEmployeeType employeeType;<br \/>\n}<br \/>\nclass EmployeeTest {<br \/>\npublic static void main(String\u2026 args) {<br \/>\nnew Employee().employeeType = EmployeeType.PERMANENT;<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\nIt is a good practise to keep enum constants in upper case letters though it is not required. We can declare enum types in separate classes. We can also use enums as class members, however we cannot declare enums in a method. Another important thing to note that, each of the enum constants are actually instances of enum types. In our example, each of the enum types PERMANENT, CONTRACT, FULLTIME and PARTTIME are instances of type EmployeeType.<\/p>\n<p>Define methods, variables and constructors in Enums<br \/>\nFor example, if we want to maintain salary information for the each of the employee types in our previous example, enums constructors are really useful in this kind of scenarios. The following code example demonstrates how salary information can be assigned to an enum type.<br \/>\n<code><br \/>\npublic enum EmployeeType {<\/code><\/p>\n<p>PERMANENT(1000), CONTRACT(1200), FULLTIME(1500), PARTTIME(500);<\/p>\n<p>private int salary;<\/p>\n<p>public int getSalary() {<br \/>\nreturn salary;<br \/>\n}<\/p>\n<p>EmployeeType(int salary) {<br \/>\nthis.salary = salary;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>As we noticed, we have an instance variable called \u201csalary\u201d in the enum declaration with getter method getSalary(). We also have enum constructor which takes salary and assigns the value to the instance variable salary. We are making use of this constructor at the beginning and creating our employee types by passing the salary information.<\/p>\n<p>Now, here is our Employee class which makes use of the just created enum EmployeeType. This is pretty simple class.<br \/>\n<code><br \/>\npublic class Employee {<br \/>\nEmployeeType employeeType;<br \/>\n}<br \/>\n<\/code><br \/>\nThe following code example creates an employee instance and gets salary information based on the enum type provided.<br \/>\n<code><br \/>\npublic class EnumTest {<br \/>\npublic static void main(String... args) {<br \/>\nEmployee permanentEmployee = new Employee();<br \/>\npermanentEmployee.employeeType = EmployeeType.PERMANENT;<br \/>\nSystem.out.println(permanentEmployee.employeeType.getSalary());<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\n<strong>Output<\/strong><br \/>\n1000<\/p>\n<p>As we see, we are able to fetch salary information based on the enum type provided. As a matter fact, we can have as much relevant information as possible kept in the enums for usage at later point.<\/p>\n<h2>Iterating enum values<\/h2>\n<p>If we want to iterate through all the types of an enum along with values we can use static method values() which returns an array of enum values in the order of declaration. The following code snippet iterates through enum values and prints enum values.<br \/>\n<code><br \/>\nfor (EmployeeType empType : EmployeeType.values()) {<br \/>\nSystem.out.println(empType +\" : \"+ empType.getSalary());<br \/>\n}<br \/>\n<\/code><br \/>\n<strong>Output<\/strong><br \/>\nPERMANENT : 1000<br \/>\nCONTRACT : 1200<br \/>\nFULLTIME : 1500<br \/>\nPARTTIME : 500<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enumerated data types (or enums for short) are newly introduced in Java 5.0. Enums will let us choose a value from a pre-defined list of values. Each value in this pre-defined list are called \u201cenums\u201d. Using enums we can restrict the values thus by reducing bugs in our code. Suppose, if we are developing payroll application, we need to have certain employee types. For example, permanent employee, contract employee, full time employee and part-time employee etc. In order to provide these employee types, we can create an enum with these pre-defined employee types as shown below: enum EmployeeType { PERMANENT, [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[739,967],"class_list":["post-7912","post","type-post","status-publish","format-standard","hentry","category-java","tag-enumerated-data-types","tag-java-3"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":23,"uagb_excerpt":"Enumerated data types (or enums for short) are newly introduced in Java 5.0. Enums will let us choose a value from a pre-defined list of values. Each value in this pre-defined list are called \u201cenums\u201d. Using enums we can restrict the values thus by reducing bugs in our code. Suppose, if we are developing payroll&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/7912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=7912"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/7912\/revisions"}],"predecessor-version":[{"id":76007,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/7912\/revisions\/76007"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=7912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=7912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=7912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}