@@ -9,11 +9,12 @@ subheader_select: tutorials
99main_css_id : learn
1010toc :
1111- What are enums? {intro}
12- - Accessing and comparing enums {accessing}
12+ - Accessing, evaluating, and comparing enums {accessing}
1313- Adding members to enums {members}
1414- Special methods {functionality}
15- - Using enums for singletons {singletons}
15+ - Using enums as singletons {singletons}
1616- Abstract methods in enums {abstract}
17+ - Conclusion {conclusion}
1718description : " Working with enums."
1819last_update : 2023-09-28
1920author : ["DanielSchmid"]
@@ -35,10 +36,10 @@ public enum DayOfWeek {
3536}
3637```
3738
38- All enums implicitely extend [ ` java.lang.Enum ` ] ( javadoc:Enum ) and cannot have any subclasses.
39+ All enums implicitly extend [ ` java.lang.Enum ` ] ( javadoc:Enum ) and cannot have any subclasses.
3940
4041<a id =" accessing " >  ; </a >
41- ## Accessing and comparing enums
42+ ## Accessing, evaluating, and comparing enums
4243
4344The values of an enum can be used as constants.
4445In order to check whether two instances of an enum are the same, the ` == ` operator can be used.
@@ -56,28 +57,22 @@ It is also possible to use `switch` for performing actions depending on the valu
5657DayOfWeek someDay = DayOfWeek . FRIDAY ;
5758
5859switch (someDay) {
59- case MONDAY :
60+ case MONDAY - >
6061 System . out. println(" The week just started." );
61- break ;
62- case TUESDAY :
63- case WEDNESDAY :
64- case THURSDAY :
62+ case TUESDAY , WEDNESDAY , THURSDAY - >
6563 System . out. println(" We are somewhere in the middle of the week." );
66- break ;
67- case FRIDAY :
64+ case FRIDAY - >
6865 System . out. println(" The weekend is near." );
69- break ;
70- case SATURDAY :
71- case SUNDAY :
66+ case SATURDAY , SUNDAY - >
7267 System . out. println(" Weekend" );
73- break ;
74- default :
68+ default - >
7569 throw new AssertionError (" Should not happen" );
7670}
7771```
7872
7973With [ Switch Expressions] ( id:lang.classes-objects.switch-expression ) ,
8074the compiler can check whether all values of the enum are handled.
75+ If any possible value is missing in a switch expression, there will be a compiler error.
8176``` java
8277DayOfWeek someDay = DayOfWeek . FRIDAY ;
8378
@@ -117,7 +112,7 @@ public enum DayOfWeek {
117112<a id =" functionality " >  ; </a >
118113## Special methods
119114
120- All enums have a few methods that are added implicitely .
115+ All enums have a few methods that are added implicitly .
121116
122117For example, the method ` name() ` is present in all enum instances and can be used to get the name of the enum constant.
123118Similarly, a method named ` ordinal() ` returns the position of the enum constant in the declaration.
@@ -133,8 +128,32 @@ DayOfWeek[] days = DayOfWeek.values(); // all days of the week
133128DayOfWeek monday = DayOfWeek . valueOf(" MONDAY" );
134129```
135130
131+ Furthermore, enums implement the interface [ ` Comparable ` ] ( javadoc:Comparable ) .
132+ By default, enums are ordered according to their ordinal number
133+ i.e. in the order of occurrence of the enum constant.
134+ This allows for comparing instances of enums as well as sorting or searching.
135+
136+ ``` java
137+ public void compareDayOfWeek(DayOfWeek dayOfWeek){
138+ int comparison = dayOfWeek. compareTo(DayOfWeek . WEDNESDAY );
139+ if ( comparison < 0 ) {
140+ System . out. println(" It's before the middle of the work week." );
141+ } else if (comparison > 0 ){
142+ System . out. println(" It's after the middle of the work week." );
143+ } else {
144+ System . out. println(" It's the middle of the work week." );
145+ }
146+ }
147+ ```
148+
149+ ``` java
150+ List<DayOfWeek > days = new ArrayList<> (List . of(DayOfWeek . FRIDAY , DayOfWeek . TUESDAY , DayOfWeek . SATURDAY ));
151+ Collections . sort(days);
152+ ```
153+
154+
136155<a id =" singletons " >  ; </a >
137- ## Using enums for singletons
156+ ## Using enums as singletons
138157
139158Since enums can only have a specific number of instances, it is possible to create a singleton by creating an enum with only a single enum constant.
140159``` java
@@ -165,4 +184,18 @@ enum MyEnum {
165184
166185 abstract void doSomething ();
167186}
168- ```
187+ ```
188+
189+ <a id =" conclusion " >  ; </a >
190+ ## Conclusion
191+
192+ All in all, enums provide a simple and safe way of limiting the instances of a type
193+ while preserving most of the flexibilities of classes.
194+
195+ However, care should be taken when using enums where the number (or names) of instances is subject to change.
196+ Whenever enum constants are changed, there may be compilation errors, runtime errors or other inconsistencies
197+ due to other code expecting the original version of the enum.
198+ This is especially important in cases where the enum is also used by other people's code.
199+
200+ Furthermore, it might be worth considering to use other options
201+ in case of many instances since listing many instances at a single location in code can be inflexible.
0 commit comments