Java Enum:
Enum declaration Example 1:
Enum declaration Example 2:
- In this tutorial I will explain what is enum, how to use enum in different areas of a Java program and an example program on it.
- An Enum type is a special data type which is added in Java 1.5 version. It is an abstract class in Java API which implements Cloneable and Serializable interfaces. It is used to define collection of constants. When you need a predefined list of values which do not represent some kind of numeric or textual data, at this moment, you have to use an enum.
- Common examples include days of week (values of SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY), directions, and months in a year. Etc.
- You can declare simple Java enum type with a syntax that is similar to the Java class declaration syntax. Let’s look at several short Java enum examples to get you started.
Enum declaration Example 1:
- public enum Day {
- SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
- }
Enum declaration Example 2:
- public enum Month{
- JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
- OCTOBER, NOVEMBER, DECEMBER
- }
- Enums are constants; they are by default static and final. That’s why the names of an enum type's fields are in uppercase letters.
- Make a note that the enum keyword which is used in place of class or interface. The Java enum keyword signals to the Java compiler that this type definition is an enum.
You can refer to the constants in the enum above like this:
- Here the ‘day’ variable is of the type ‘Day’ which is the Java enum type defined in the example above. The ‘day’ variable can take one of the ‘Day’ enum constants as value (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY). In this case ‘day’ is set to MONDAY.
- If you use enum instead of integers or String codes, you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.
- How to use a Java enum type in various areas in a Java Program:
- We have seen how to declare simple Java enum types, let’s take a look at how to use them in various areas. We have to use a Java enum type in a variety of situations, including in a Java 5 for loop, in a switch statement, in an if/else statement, and more. For simple, Enum comparison, there are 3 ways to do it. They are, Switch-Case Statement, == Operator, .equals() method. Like that there are other places where you have to use Enum.
- Let's take a look at how to use our simple enum types with each of these Java constructs.
- Enum in IF statement
- Enum in Switch statement
- Enum Iteration in for-each loop
- Enum Fields
- Enum Methods
1. Enum in IF statements:
- We know Java Enums are constants. Sometimes, we have a requirement to compare a variable of Enum constant against the possible other constants in the Enum type. At this moment, we have to use IF statement as follows.
- Day day = ----- //assign some Day constants to it.
- If(day ==Day.MONDAY){
- ….//your code
- } else if (day == Day.TUESDAY){
- ….//your code
- } else if (day == Day.WEDNESDAY){
- ….//your code
- } else if (day == Day. THURSDAY){
- ….//your code
- } else if (day == Day.FRIDAY){
- ….//your code
- } else if (day == Day.SATURDAY){
- ….//your code
- } else if (day == Day.SUNDAY){
- ….//your code
- }
- Here, you can use “.equals()” instead of “==”. But, my preference is to use “==” because, it will never throws NullPointerException, safer at compile-time, runtime and faster.
- The code snippet compares the ‘day’ variable against each of the possible other Enum constants in the ‘Day’ Enum.
2. Enums in Switch Statements:
- Just assume that your Enum have lot of constants and you need to check a variable against the other values in Enum. For a small Enum IF Statement will be OK. If you use same IF statement for lot of Enum constants then our program will be increased and it is not a standard way to write a Java program. At this Situation, use Switch Statement instead of IF statement.
- You can use enums in switch statements as follows:
- Day day = ... //assign some Day constant to it
- switch (day) {
- case SUNDAY : //your code; break;
- case MONDAY //your code; break;
- case TUESDAY : //your code; break;
- case WEDNESDAY : //your code; break;
- case THURSDAY: //your code; break;
- case FRIDAY : //your code; break;
- case SATURDAY : //your code; break;
- }
- Here, give your code in the comments, “//your code”. The code should be a simple Java operation or a method call..etc
A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.
ReplyDeleteClick here:
python online training
Click here:
python training in usa
A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
ReplyDeleteBlueprism training in Chennai
Blueprism training in Bangalore
Blueprism training in Pune
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeleteData Science training in Chennai | Data science training in bangalore
Data science training in pune | Data science online training
Data Science Interview questions and answers
This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
ReplyDeletejava training in chennai | java training in bangalore
java interview questions and answers | core java interview questions and answers
I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.
ReplyDeleteangularjs-Training in tambaram
angularjs-Training in sholinganallur
angularjs-Training in velachery
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in btm
Thanks for the great content! keep uploading more post.
ReplyDeleteselenium testing training
best training institute for selenium in chennai
Selenium Training in Chennai
Big Data Training in Chennai
web designing course
web designing courses
web designing training in chennai
Brief explanation, waiting for more updates.
ReplyDeletePython Training Institute in Chennai
Python Training in Velachery
Robotics Process Automation Training in Chennai
Angularjs Training institute in Chennai
Data Science Training in Chennai
Data Science Course in Chennai
ReplyDeleteIt seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium training in bangalore
I and my friends were going through the nice, helpful tips from the blog then the sudden came up with an awful suspicion I never expressed respect to the website owner for those secrets.
ReplyDeletepython training Course in chennai
python training in Bangalore
Python training institute in bangalore
Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
ReplyDeleteAWS Training in Chennai | Best AWS Training in Chennai | AWS Training Course in Chennai
Data Science Training in Chennai | Best Data Science Training in Chennai | Data Science Course in Chennai
No.1 Python Training in Chennai | Best Python Training in Chennai | Python Course in Chennai
RPA Course Training in Chennai | Best RPA Training in Chennai
No.1 RPA Training in Chennai | Best RPA Training in Chennai | RPA Course in Chennai
Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
This is very great thinks. It was very comprehensive post and powerful concept. Thanks for your sharing with us. Keep it up..good luck.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
Thanks for your marvelous and wonderful post.keep doing on.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.data science course in Hyderabad
ReplyDeleteAwesome blog it's very nice and useful i got many more information it's really nice i like your blog
ReplyDeleteAI Training in Hyderabad
Thanks for posting this! I searched on google and this page was the first result, very good to know.It's Pleasant to Visit your site, Such a Informative Articles Are Really Interesting.Keep Blogging...
ReplyDeleteJava Training in Chennai
Java Training in Velachery
Java Training in Tambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital Marketing Training in Annanagar
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
ReplyDeletePython Training in Chennai
Python Training in Velachery
Python Training in Tambaram
Python Training in Porur
Python Training in Omr
Python Training in Annanagar
coin haber - koin haber - kripto para haberleri - coin haber - instagram video indir - instagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir - instagram takipçi satın al
ReplyDeleteI visit first here, and this information is useful and informative. Thanks for sharing this blog with us. If you want to become a data science expert, then follow the below link.
ReplyDeleteData Science Training Institute in Hyderabad
en son çıkan perde modelleri
ReplyDeleteNumara onay
Türk telekom mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
AÅžK ROMANLARI
This comment has been removed by the author.
ReplyDelete