Skip to content

Commit 50010a7

Browse files
committed
formatting, additional comments, TOC, etc.
1 parent 3af900a commit 50010a7

File tree

2 files changed

+74
-55
lines changed

2 files changed

+74
-55
lines changed

app/data/authors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@
8181
photo_url: https://danthe1st.github.io/img/Daniel.jpg
8282
website: https://danthe1st.github.io/
8383
description: |
84-
Daniel is a Java Developer from Austria who is also managing an online Java User Group.
84+
Daniel is a Java Developer from Austria who is also managing the <a href="https://discordjug.net/">Discord Java Community</a>.

app/pages/learn/01_tutorial/03_getting-to-know-the-language/04_classes_objects/01_enums.md

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,46 @@ group: classes_objects
77
layout: learn/tutorial-group.html
88
subheader_select: tutorials
99
main_css_id: learn
10+
toc:
11+
- What are enums? {intro}
12+
- Accessing and comparing enums {accessing}
13+
- Adding members to enums {members}
14+
- Special methods {functionality}
15+
- Using enums for singletons {singletons}
16+
- Abstract methods in enums {abstract}
1017
description: "Working with enums."
1118
last_update: 2023-09-28
1219
author: ["DanielSchmid"]
1320
---
21+
<a id="intro">&nbsp;</a>
1422
## What are enums?
23+
1524
Enums are classes where all instances are known to the compiler.
1625
They are used for creating types that can only have few possible values.
1726

1827
Enums can be created similar to classes but use the `enum` keyword instead of `class`.
19-
In the body, there is a list of instances of the enum named enum constants which are seperated by `,`.
28+
In the body, there is a list of instances of the enum called enum constants which are seperated by `,`.
2029
No instances of the enum can be created outside of enum constants.
2130

2231
```java
2332
public enum DayOfWeek {
24-
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
33+
// enum constant are listed here:
34+
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
2535
}
2636
```
2737

2838
All enums implicitely extend [`java.lang.Enum`](javadoc:Enum) and cannot have any subclasses.
2939

40+
<a id="accessing">&nbsp;</a>
3041
## Accessing and comparing enums
42+
3143
The values of an enum can be used as constants.
3244
In order to check whether two instances of an enum are the same, the `==` operator can be used.
3345
```java
3446
DayOfWeek weekStart = DayOfWeek.MONDAY;
3547

3648
if (weekStart == DayOfWeek.MONDAY) {
37-
System.out.println("The week starts on Monday.");
49+
System.out.println("The week starts on Monday.");
3850
}
3951
```
4052

@@ -44,23 +56,23 @@ It is also possible to use `switch` for performing actions depending on the valu
4456
DayOfWeek someDay = DayOfWeek.FRIDAY;
4557

4658
switch (someDay) {
47-
case MONDAY:
48-
System.out.println("The week just started.");
49-
break;
50-
case TUESDAY:
51-
case WEDNESDAY:
52-
case THURSDAY:
53-
System.out.println("We are somewhere in the middle of the week.");
54-
break;
55-
case FRIDAY:
56-
System.out.println("The weekend is near.");
57-
break;
58-
case SATURDAY:
59-
case SUNDAY:
60-
System.out.println("Weekend");
61-
break;
62-
default:
63-
throw new AssertionError("Should not happen");
59+
case MONDAY:
60+
System.out.println("The week just started.");
61+
break;
62+
case TUESDAY:
63+
case WEDNESDAY:
64+
case THURSDAY:
65+
System.out.println("We are somewhere in the middle of the week.");
66+
break;
67+
case FRIDAY:
68+
System.out.println("The weekend is near.");
69+
break;
70+
case SATURDAY:
71+
case SUNDAY:
72+
System.out.println("Weekend");
73+
break;
74+
default:
75+
throw new AssertionError("Should not happen");
6476
}
6577
```
6678

@@ -70,15 +82,16 @@ the compiler can check whether all values of the enum are handled.
7082
DayOfWeek someDay = DayOfWeek.FRIDAY;
7183

7284
String text = switch (someDay) {
73-
case MONDAY -> "The week just started.";
74-
case TUESDAY, WEDNESDAY, THURSDAY -> "We are somewhere in the middle of the week.";
75-
case FRIDAY -> "The weekend is near.";
76-
case SATURDAY, SUNDAY -> "Weekend";
85+
case MONDAY -> "The week just started.";
86+
case TUESDAY, WEDNESDAY, THURSDAY -> "We are somewhere in the middle of the week.";
87+
case FRIDAY -> "The weekend is near.";
88+
case SATURDAY, SUNDAY -> "Weekend";
7789
};
7890

7991
System.out.println(text);
8092
```
8193

94+
<a id="members">&nbsp;</a>
8295
## Adding members to enums
8396

8497
Just like classes, enums can have constructors, methods and fields.
@@ -87,63 +100,69 @@ Arguments to the constructor are passed in parenthesis after the declaration of
87100

88101
```java
89102
public enum DayOfWeek {
90-
MONDAY("MON"), TUESDAY("TUE"), WEDNESDAY("WED"), THURSDAY("THU"), FRIDAY("FRI"), SATURDAY("SAT"), SUNDAY("SUN");
91-
92-
private final String abbreviation;
93-
94-
DayOfWeek(String abbreviation) {
95-
this.abbreviation = abbreviation;
96-
}
97-
98-
public String getAbbreviation() {
99-
return abbreviation;
100-
}
103+
MONDAY("MON"), TUESDAY("TUE"), WEDNESDAY("WED"), THURSDAY("THU"), FRIDAY("FRI"), SATURDAY("SAT"), SUNDAY("SUN");
104+
105+
private final String abbreviation;
106+
107+
DayOfWeek(String abbreviation) {
108+
this.abbreviation = abbreviation;
109+
}
110+
111+
public String getAbbreviation() {
112+
return abbreviation;
113+
}
101114
}
102115
```
103116

117+
<a id="functionality">&nbsp;</a>
104118
## Special methods
119+
105120
All enums have a few methods that are added implicitely.
106121

107122
For example, the method `name()` is present in all enum instances and can be used to get the name of the enum constant.
108123
Similarly, a method named `ordinal()` returns the position of the enum constant in the declaration.
109124
```java
110-
System.out.println(DayOfWeek.MONDAY.name());//MONDAY
111-
System.out.println(DayOfWeek.MONDAY.ordinal());//0 because MONDAY is the first constant in the DayOfWeek enum
125+
System.out.println(DayOfWeek.MONDAY.name()); // prints "MONDAY"
126+
System.out.println(DayOfWeek.MONDAY.ordinal()); // prints "0" because MONDAY is the first constant in the DayOfWeek enum
112127
```
113128

114129
Aside from instance methods, there are also static methods added to all enums.
115130
The method `values()` returns an array containing all instances of the enum and the method `valueOf(String)` can be used to get a specific instance by its name.
116131
```java
117-
DayOfWeek[] days = DayOfWeek.values();//all days of the week
132+
DayOfWeek[] days = DayOfWeek.values(); // all days of the week
118133
DayOfWeek monday = DayOfWeek.valueOf("MONDAY");
119134
```
120135

136+
<a id="singletons">&nbsp;</a>
121137
## Using enums for singletons
138+
122139
Since 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.
123140
```java
124141
public enum SomeSingleton {
125-
INSTANCE;
126-
//fields, methods, etc.
142+
INSTANCE;
143+
//fields, methods, etc.
127144
}
128145
```
129146

147+
<a id="abstract">&nbsp;</a>
130148
## Abstract methods in enums
149+
131150
Even though enums cannot be extended, they can still have `abstract` methods. In that case, an implementation must be present in each enum constant.
132151
```java
133152
enum MyEnum {
134-
A() {
135-
@Override
136-
void doSomething() {
137-
System.out.println("a");
138-
}
139-
},
140-
B() {
141-
@Override
142-
void doSomething() {
143-
System.out.println("b");
144-
}
145-
};
146-
147-
abstract void doSomething();
153+
A() {
154+
@Override
155+
void doSomething() {
156+
System.out.println("a");
157+
}
158+
},
159+
B() {
160+
@Override
161+
void doSomething() {
162+
System.out.println("b");
163+
}
164+
};
165+
166+
abstract void doSomething();
148167
}
149168
```

0 commit comments

Comments
 (0)