-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenumeration.html
More file actions
192 lines (151 loc) · 5.85 KB
/
enumeration.html
File metadata and controls
192 lines (151 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!--
~ Author: @tridib2003
~ Repository: https://github.com/tridib2003/levelupjava
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enumeration | Level-Up Java</title>
<link href="styles.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</nav>
<h1 class="h1-basic">Enumeration</h1>
<div class="desc-container desc-container-center desc-std">
<hr>
<ul>
<li>
<p>
<strong><i>Enumeration</i></strong> is a group of predefined constants that defines a new data type.
</p>
</li>
<li>
<p>
The <i>enum</i> (enumeration) variable can only store one of those values that have been predefined for it.
</p>
</li>
<li>
<p>
<i>Enums</i> being constants, the values of an enum set are in uppercase letters.
</p>
</li>
<li>
<p>
<i>Enums</i> in Java defines a class type.
Thus, an enumeration in Java can have constructors, methods, instance variables, and even implement interfaces.
</p>
</li>
<li>
<p>
An enumeration is created using the <strong>enum</strong> keyword.
The listed values of an enum are called <i>enumeration constants</i>.
</p>
</li>
<br>
<div class="syntax-container syntax-container-center syntax-std">
<p style="font-weight: bold;">
General syntax:
</p>
<xmp>
public enum Planets{
MERCURY, VENUS, EARTH, MARS,
JUPITER, SATURN, URANUS, NEPTUNE
}
</xmp>
</div>
<li>
<p>
We can compare two enumeration constants using the <i>==</i> operator.
</p>
</li>
<li>
<p>
An enum value can be used to control the flow of a <i>switch</i> statement.
</p>
</li>
<li>
<p>
Java compiler internally creates a static and final class for an <i>enum</i>.
</p>
</li>
<li>
<p>
All <i>enums</i> contains some predefined methods like <strong>values()</strong> and <strong>valueOf()</strong>.
The <strong>values()</strong> method returns an array containing all the enumeration constants.
The <strong>valueOf()</strong> method returns the enumeration constant which matches the string parameter being passed to the method.
</p>
</li>
</ul>
</div>
<br><br><br>
<div class="table-container table-container-center">
<table class="table-code-links">
<tr>
<td>
Demonstrating enumerations
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Enumeration/EnumDemo.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Understanding <strong>values()</strong> and <strong>valuesOf()</strong> methods
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Enumeration/EnumDemo2.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
</table>
</div>
<br><br><br>
<footer class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</footer>
</body>
</html>