Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
Now Reading
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
If you are a programming enthusiast who wants to learn more about Enum in Java, you have come to the right place. Enum in Java is a special type of data or constant. It is an integral part of Java, introduced first in Java 5.
The following tutorial deals with questions such as what is Enumeration in Java, examples of Enumeration in Java, abstract methods in Enum, and a lot more. Read on to master Enumeration in Java!
Enumeration in Java is a data type that defines a set of named constants. It is used to create a list of values assigned to a type. This can then be used in the program.
In Java, Enumerations are represented using the "enum" keyword.
Here is an example of how to create an Enumeration:
enum Days {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
In this example, the "Days" Enumeration contains seven named constants: MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY. We can use these constants in the program to represent the days of the week.
Enumerations are helpful because they allow you to define a set of values that are meaningful in the context of your program. They also help prevent errors by ensuring only valid values are used. We can also use Enumerations to switch statements. This makes it easy to write code that handles different cases based on the value of an Enumeration.
Enumerations are helpful in programming for several reasons. Here are some of them:
Readability and Maintainability: Enumerations can increase the readability of a code. They can also make it easier to understand. Instead of using integer or string constants that are unclear or not memorable, using named constants that are meaningful in the program's context can help improve a code's readability. Moreover, if changes need to be made to the set of constants, it is much easier to do so with an Enumeration.
Type Safety: Type safety assists in preventing programming errors. For instance, if a method requires an Enumeration value as an argument, it cannot be accidentally passed an invalid value.
Compile-Time Checking: The Java compiler verifies that Enumeration values are used correctly. These checks could include ensuring that each value in the Enumeration is declared only once. It also verifies that the values are of the proper type. This can aid in detecting faults before they cause difficulties during runtime.
Switch Statements: Enumerations can be used in switch statements to make code shorter and easier to read. Instead of utilizing a sequence of if-else statements, a switch statement can be used to conduct different actions based on the value of an Enumeration.
Here is a simple Enum example in Java:
public enum DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class Main {
public static void main(String[] args) {
DayOfWeek today = DayOfWeek.WEDNESDAY;
System.out.println("Today is " + today);
}
}
The “values()” method is a built-in method in Java Enum. This returns an array of all the values defined in the Enumeration.
The “values()” method provides an easy way to access all the values defined in the Enumeration. This is done without manually creating an array of all the values.
Here is an example of Enum with “values()” in Java:
public enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}
public class ExampleEnum {
public static void main(String[] args) {
Month[] months = Month.values();
for (Month month : months) {
System.out.println(month);
}
}
}
The “valueOf()” method is a built-in method in Java Enum. This returns the Enum constant of the specified string value.
The “valueOf()” method provides a convenient way to convert a string value into an Enum constant. This can be useful in many cases. For instance, when reading input from a user or a file, we need to convert a string value into an Enum constant.
Here is an example of how to use the “valueOf()” method in Java Enum with string values:
public enum Color {
RED, GREEN, BLUE
}
public class ExampleEnum {
public static void main(String[] args) {
String colorString = "GREEN";
Color color = Color.valueOf(colorString);
System.out.println("The color is " + color);
}
}
The “ordinal()” method is a built-in method in Java Enum. This returns the ordinal value of an Enum constant. The ordinal value is an integer that represents the position of the Enum constant in the Enumeration, starting from zero.
The purpose of the “ordinal()” method is to provide a convenient way to get the position of an Enum constant in the Enumeration. This can be useful in many cases. For instance, we need to perform comparisons or sorting operations on Enum constants based on their position in the Enumeration.
Here is an example of how to use the “ordinal()” method:
public enum Weekday {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
}
public class ExampleEnum {
public static void main(String[] args) {
Weekday day = Weekday.WEDNESDAY;
int ordinal = day.ordinal();
System.out.println("The ordinal value of " + day + " is " + ordinal);
}
}
You can initialize specific values to the Enum constants. This can be done using a constructor. Here is an example:
public enum Color {
RED(255, 0, 0),
GREEN(0, 255, 0),
BLUE(0, 0, 255);
private int r;
private int g;
private int b;
private Color(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public int getRed() {
return r;
}
public int getGreen() {
return g;
}
public int getBlue() {
return b;
}
}
public class ExampleEnum {
public static void main(String[] args) {
Color color = Color.GREEN;
System.out.println("The color is " + color);
System.out.println("The RGB values are " + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue());
}
}
Here is an example of how to specify an initial value to the Enum constants:
public enum Size {
SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
private final String abbreviation;
private Size(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
}
public class ExampleEnum {
public static void main(String[] args) {
Size size = Size.SMALL;
System.out.println("The size is " + size);
System.out.println("The abbreviation is " + size.getAbbreviation());
}
}
An Enum can also be used in inheritance. Here is an example:
public enum Shape {
CIRCLE {
@Override
double calculateArea(double arg1, double arg2) {
double radius = arg1;
return Math.PI * radius * radius;
}
},
RECTANGLE {
@Override
double calculateArea(double arg1, double arg2) {
double length = arg1;
double width = arg2;
return length * width;
}
},
TRIANGLE {
@Override
double calculateArea(double arg1, double arg2) {
double base = arg1;
double height = arg2;
return 0.5 * base * height;
}
};
abstract double calculateArea(double arg1, double arg2);
}
public class ShapeCalculator {
public static void main(String[] args) {
double circleArea = Shape.CIRCLE.area(5.0);
double rectangleArea = Shape.RECTANGLE.area(4.0, 6.0);
double triangleArea = Shape.TRIANGLE.area(3.0, 4.0);
System.out.println("Circle area: " + circleArea);
System.out.println("Rectangle area: " + rectangleArea);
System.out.println("Triangle area: " + triangleArea);
}
}
Enums can also be used in control flow statements in Java. Here is an example:
public enum DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
public class ExampleEnum {
public static void main(String[] args) {
DayOfWeek day = DayOfWeek.FRIDAY;
switch (day) {
case MONDAY:
System.out.println("It's Monday");
break;
case TUESDAY:
System.out.println("It's Tuesday");
break;
case WEDNESDAY:
System.out.println("It's Wednesday");
break;
case THURSDAY:
System.out.println("It's Thursday");
break;
case FRIDAY:
System.out.println("It's Friday");
break;
case SATURDAY:
System.out.println("It's Saturday");
break;
case SUNDAY:
System.out.println("It's Sunday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
Here is an example of using a for-loop statement with an Enum in Java:
public class EnumExample {
public enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}
public static void main(String[] args) {
for (Month month : Month.values()) {
System.out.println(month);
}
}
}
Here is an example of how to use some of the standard Enum methods in Java:
public enum Size {
SMALL, MEDIUM, LARGE, XLARGE;
public static Size fromString(String sizeString) {
for (Size size : Size.values()) {
if (size.toString().equalsIgnoreCase(sizeString)) {
return size;
}
}
throw new IllegalArgumentException("No Enum constant Size." + sizeString);
}
}
Here is an example of how to use an Enum in a switch statement in Java:
public Enum DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
public class Example {
public static void main(String[] args) {
DayOfWeek day = DayOfWeek.THURSDAY;
switch (day) {
case MONDAY:
System.out.println("Today is Monday");
break;
case TUESDAY:
System.out.println("Today is Tuesday");
break;
case WEDNESDAY:
System.out.println("Today is Wednesday");
break;
case THURSDAY:
System.out.println("Today is Thursday");
break;
case FRIDAY:
System.out.println("Today is Friday");
break;
case SATURDAY:
System.out.println("Today is Saturday");
break;
case SUNDAY:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Invalid day of week");
}
}
}
We hope that this tutorial will be helpful to programming enthusiasts who are looking for reliable sources to master Enum in Java. To be a professional computer science expert, you must seek guidance from experts in the said field.
Several online learning platforms, such as upGrad, offer well-designed programming courses. Enrol in their courses to unlock the best learning benefits!
1. Can you define an Enum inside a class in Java?
Yes, you can define an Enum inside a class in Java. This can be useful if the Enum is closely related to the class. Also, the Enum should not be used outside of it.
2. Can Enums inherit from other Enums in Java?
No, Enums cannot inherit from other Enums in Java. However, you can define a class hierarchy and have an Enum implement an interface or extend a class.
3. Are Enums mutable or immutable in Java?
Enums are immutable in Java. This means that their values cannot be changed once they are defined. However, it is possible to make an Enum mutable. It can be done by adding setter methods or making its fields public.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...