{"id":218,"date":"2006-11-21T17:24:02","date_gmt":"2006-11-21T17:24:02","guid":{"rendered":"http:\/\/kubasek.com\/blog\/pragmatic_craftsman\/?p=218"},"modified":"2006-11-21T17:24:02","modified_gmt":"2006-11-21T17:24:02","slug":"java-5-enums","status":"publish","type":"post","link":"https:\/\/pragmaticcraftsman.kubasek.com\/2006\/11\/21\/java-5-enums\/","title":{"rendered":"Java 5 Enums"},"content":{"rendered":"<p>Java 5 SE, in my opinion was a great release. While reading a blog post, <a href=\"http:\/\/softarc.blogspot.com\/2006\/11\/java-5-gems-and-duds_20.html\">Java 5 &#8211; The Gems and the Duds<\/a>, the author gives thumbs up to the concurrency package, generics, CachedRowSet, and annotations; he gives thumbs down to autoboxing. And he&#8217;s not sure about varargs and enums. Very good post.  I generally agree with the author. But&#8230;<\/p>\n<p>I happen to like enums. I have used them several times already and they did a great job for me: made the code easier to understand and the code more robust (type safety). What are they good for?<\/p>\n<p>I have used enums to encapsulate the different types. Pre Java 5, we used to have a lot of String constants defined. What that does it puts a lot of unrelated things together. It&#8217;s hard to see where each particular constant belongs to. There is also no type safety, as the constant can be substituted with any value.<\/p>\n<p>No more. I can now define an enum. It nicely encloses related types. It gives me type safety. They make your code more readable. They make your comparisons easy &#8212; you can use == with confidence.<\/p>\n<p>A simple example,enum DayOfWeek {MON(1), TUE(2), WED(3)&#8230; \/* need to define a constructor in this case *\/ }<\/p>\n<p>In code, you would no longer rely on integers 1-7 or strings for days, you would get a DayOfWeek param and you would be sure that you actually get the right value. You could also define a utility method inside the enum, getByDayNumber(&#8230;) and get the day that way.<\/p>\n<p>In my opinion, a great addition.<\/p>\n<p><strong>Reference<\/strong><a href=\"http:\/\/softarc.blogspot.com\/2006\/11\/java-5-gems-and-duds_20.html\">Java 5 &#8211; The Gems and the Duds<\/a>,  The Art and Craft of Great Software Architecture and Development blog<\/p>\n<p><a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/guide\/language\/enums.html\">Enums<\/a>, java.sun.com article<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 5 SE, in my opinion was a great release. While reading a blog post, Java 5 &#8211; The Gems and the Duds, the author gives thumbs up to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-218","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts\/218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":0,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"wp:attachment":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}