tag:blogger.com,1999:blog-8712770457197348465.post5373796023394121476..comments2024-03-19T01:04:48.783-07:00Comments on Javarevisited: Java Enum Tutorial: 10 Examples of Enum in Javajavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger66125tag:blogger.com,1999:blog-8712770457197348465.post-88607076286216894612021-03-20T05:21:27.749-07:002021-03-20T05:21:27.749-07:00Hello Wali, glad that you like this Enum tutorial,...Hello Wali, glad that you like this Enum tutorial, Yes, I try to regularly update my articles. I also thankful to my readers which point out and alert for any information which needs to be update. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28888748910168867912021-03-18T10:23:48.984-07:002021-03-18T10:23:48.984-07:00This is a really good write up on enums, clear and...This is a really good write up on enums, clear and to the point. This article must've been updated at some point, but it's still relevant and useful. Walinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50797441894066212562017-07-23T03:42:46.645-07:002017-07-23T03:42:46.645-07:00Nice and very clear explanationNice and very clear explanationGaneshhttps://www.blogger.com/profile/16753850250071898475noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59582771619390256042017-05-11T07:41:36.241-07:002017-05-11T07:41:36.241-07:00Hello Arshad, the error is because just writing va...Hello Arshad, the error is because just writing value is illegal e.g. 5; you need to store the value into a variable e.g.<br />Season s = Season.SUCCESS; <br />this will solve the error. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4146615832854656652017-05-11T06:14:50.758-07:002017-05-11T06:14:50.758-07:00enum Season {
SUCCESS(),
FAILURE() ;
Season(){
S...enum Season { <br />SUCCESS(),<br /> FAILURE() ;<br />Season(){<br />System.out.println("Hello enum");<br />}<br />}<br />class EnumExample3{<br />public static void main(String[] args) {<br />//System.out.println(Season.SUCCESS); <br />Season.SUCCESS; // this line of code give an error: not a statement<br />}}<br /><br />// why the commented out line of code giving error while Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-851895592637574902017-01-31T14:39:36.841-08:002017-01-31T14:39:36.841-08:00Hello Amol, that empty bracket mean call to a no-a...Hello Amol, that empty bracket mean call to a <a href="http://javarevisited.blogspot.sg/2014/01/why-default-or-no-argument-constructor-java-class.html" rel="nofollow">no-argument constructor</a>. Since enum can have constructor, you can also call them like that e.g. BILLBACK("bill") means call to a constructor which takes one String argument. I have shown examples of Enum with javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22101714383516004062017-01-31T08:38:23.269-08:002017-01-31T08:38:23.269-08:00public enum FormOfPayment {
BILLBACK(), BILLBAKC2...public enum FormOfPayment {<br /> BILLBACK(), BILLBAKC2(),BILLBACK3()<br />}<br /><br />What does empty bracket means here?Amolhttps://www.blogger.com/profile/13067348890171030484noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-89552042582381911032016-09-23T10:08:06.595-07:002016-09-23T10:08:06.595-07:00Very very informative article, and also thanks use...Very very informative article, and also thanks users for great commentsAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47972838466854440922016-05-28T04:05:43.189-07:002016-05-28T04:05:43.189-07:00Good points @GOPI, well done !!Good points @GOPI, well done !!javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-5062981904944121242016-05-28T03:15:18.212-07:002016-05-28T03:15:18.212-07:00Nice article..Thank you dear...
1. You mentioned &...Nice article..Thank you dear...<br />1. You mentioned "Also, the semicolon in the first line is optional." but semicolon is optional only if enum has only well defined constants but if we miss semicolon when it has other members like methods then we must have semicolon.<br />2. If enum has other members other than constants then constants must be at 1st line inside a enum else compiler GOPIhttps://www.blogger.com/profile/09541394969420706105noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73610459556752418922016-02-17T08:53:56.156-08:002016-02-17T08:53:56.156-08:00Very good Article. Thanks for this :)Very good Article. Thanks for this :)Anonymoushttps://www.blogger.com/profile/16984895089793354164noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13663418046764878632015-09-10T21:56:02.836-07:002015-09-10T21:56:02.836-07:00@Anonymous, both are Ok, until you have less numbe...@Anonymous, both are Ok, until you have less number of enum constant. switch result in concise code (less number of lines, hence more readable) and overriding toString inside each is like open closed principle but takes more space.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14057860416097382742015-09-09T09:05:13.525-07:002015-09-09T09:05:13.525-07:00You should avoid switching within the enum. So the...You should avoid switching within the enum. So the same enum with a better toString would be:<br />public enum Currency {<br /> PENNY(1) {<br /> @Override<br /> public String toString() {<br /> return "Penny: " + super.value;<br /> }<br /> },<br /> NICKLE(5) {<br /> @Override<br /> public String toString() {<br /> return "Nickle: " + super.value;<br /> }<br /> },<br Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84178749962450776462015-05-06T12:31:09.796-07:002015-05-06T12:31:09.796-07:00Thanks,
It's make senseThanks, <br />It's make senseAnonymoushttps://www.blogger.com/profile/10265105539830532765noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-34337250727472285802014-12-21T04:48:16.211-08:002014-12-21T04:48:16.211-08:00You have written that constructors of enum type ca...You have written that constructors of enum type can only be private...<br />But I have read on docs.orale.com that, it can be private or package-private<br />.<br />.<br />Can you please re-confirm thisAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83718398178930539232014-07-02T23:27:26.752-07:002014-07-02T23:27:26.752-07:00Is it mandatory to make constructor of enum as pri...Is it mandatory to make constructor of enum as privateAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85691708571284694302014-06-04T16:26:53.483-07:002014-06-04T16:26:53.483-07:00The awesomest explanation ever. It is so detailed ...The awesomest explanation ever. It is so detailed and very well explained. Thanks a ton!Anonymoushttps://www.blogger.com/profile/02114834476865654888noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54267578686460968222014-05-14T21:44:03.672-07:002014-05-14T21:44:03.672-07:00you are a brave java developer...for this(Further ...you are a brave java developer...for this(Further Reading on Java Enum...)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-49174293074130906262014-03-16T10:16:59.312-07:002014-03-16T10:16:59.312-07:00Constructor in enum need not be emply...Anonymous ...Constructor in enum need not be emply...Anonymous is right...anyway this tutorial is really nice. please make tutorial related to thread also.Anonymoushttps://www.blogger.com/profile/11064954157717867889noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36616513269312063022014-03-16T10:15:11.347-07:002014-03-16T10:15:11.347-07:00yes constructor in enum need not to be private.
yes constructor in enum need not to be private.<br />Anonymoushttps://www.blogger.com/profile/11064954157717867889noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44825287116683432102013-11-04T22:28:20.125-08:002013-11-04T22:28:20.125-08:00Read many article on Enum but was never clarified ...Read many article on Enum but was never clarified until i read your blog.Its very nice and crisp thnks for shring it in very simple way,helped me a lot to know the versatility of Enum in JavaSourabhhttps://www.blogger.com/profile/08248850343050113209noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67987915944777618192013-09-19T03:33:16.083-07:002013-09-19T03:33:16.083-07:00constructor of enum need not to be private check i...constructor of enum need not to be private check it <br />onceAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-60653721960526410222013-07-25T00:08:31.499-07:002013-07-25T00:08:31.499-07:00Just to add my 2 cents on this wonderful article o...Just to add my 2 cents on this wonderful article on Enum, I would like to share few tips on when to use Enum in Java programs :<br /><br />1) Prefer Enum over String, for example if your method accepts String argument, which you compare against predefined one, than consider passing Enum, it will provide compile time type safety.<br /><br />2) Prefer Enum over integer constant, Similar to reasons Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14632664387370338962013-07-15T06:37:34.604-07:002013-07-15T06:37:34.604-07:00Can we use abstract keyword along with Enum in Jav...Can we use abstract keyword along with Enum in Java? I want to use abstract method with enum, how to use that?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43533541200813702382013-07-08T23:45:31.776-07:002013-07-08T23:45:31.776-07:00WOW, lots of examples from Enum. Great works guys,...WOW, lots of examples from Enum. Great works guys, including Commentators. Here is one of my Enum examples. It's from financial worlds. We have a clearer, normally banks, which clears trades and charge clearing fees ranging form $100- $500. If you are dealing with only few clearer, you can use Enum to represent them as shown below :<br /><br />private enum Clearer{<br /> BankOfAmerica(Jamesnoreply@blogger.com