Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>enum is a very powerful if we know how to use enum and when to use enum</p> <p>Case 1: When you have string contant values and you are comparing them.</p> <p>How enum helps: Crate enum of all strings and instread of comparing string compare emum. As enum compare on the basis of their hash value which is a number so it is fast then the string comparision.</p> <p>As shown in the <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html" rel="nofollow">Oracle documentation</a> they used Sunday, Monday as enum not String.</p> <pre><code>public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } </code></pre> <p>Case 2: When you have to constants and you have to compare constants because value of some other constant is based on result. for eg. You have months name and on the basis of month names you get how many days are there and start day type in this case enum helps you a lot.</p> <p>for eg. </p> <pre><code>enum MonthEnum { JAN("Jan", 31, "Sunday"), Feb("Feb", 28, "Tuesday"), March("March", 28, "Monday"); private final String monthName; private final int noOfDays; private final String startDay; private MonthEnum(final String monthName, final int noOfDays, final String startDay) { this.monthName = monthName; this.noOfDays = noOfDays; this.startDay = startDay; } public String getMonthName() { return monthName; } public int getNoOfDays() { return noOfDays; } public String getStartDay() { return startDay; } } </code></pre> <p>and you can easily get enum by passing only monthName</p> <pre><code>public static MonthEnum getMonthEnum(final String monthName) { MonthEnum month = null; for(MonthEnum monthEnum : MonthEnum.values()) { if(monthEnum.getMonthName().equalsIgnoreCase(monthName)) { month = monthEnum; } } return month; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload