Skip to content

Commit ae8ff4d

Browse files
Aman UllaAman Ulla
authored andcommitted
added enum
1 parent 3e9b889 commit ae8ff4d

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

.idea/workspace.xml

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ It is used to develop rich internet applications. It uses a light-weight user in
116116
|[Access Modifiers](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Encapsulation/AcessModifier.md)|
117117
|[Encapsulation](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Encapsulation/Encapsulation.md)|
118118
|[Arrays in Java](https://github.com/connectaman/Java_Notes_and_Programs/tree/master/src/Arrays)|
119-
|[]()|
120-
|[]()|
119+
|[String in Java](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Strings/string.md)|
120+
|[Enum]()|
121121
|[]()|
122122
|[]()|
123123
|[]()|

src/Enumeration/enum.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
### Java Enums
2+
3+
---------
4+
5+
6+
The Enum in Java is a data type which contains a fixed set of constants.
7+
8+
9+
10+
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY) , directions (NORTH, SOUTH, EAST, and WEST), season (SPRING, SUMMER, WINTER, and AUTUMN or FALL), colors (RED, YELLOW, BLUE, GREEN, WHITE, and BLACK) etc. According to the Java naming conventions, we should have all constants in capital letters. So, we have enum constants in capital letters.
11+
12+
13+
Java Enums can be thought of as classes which have a fixed set of constants (a variable that does not change). The Java enum constants are static and final implicitly. It is available since JDK 1.5.
14+
15+
16+
Enums are used to create our own data type like classes. The enum data type (also known as Enumerated Data Type) is used to define an enum in Java. Unlike C/C++, enum in Java is more powerful. Here, we can define an enum either inside the class or outside the class.
17+
18+
19+
Java Enum internally inherits the Enum class, so it cannot inherit any other class, but it can implement many interfaces. We can have fields, constructors, methods, and main methods in Java enum.
20+
21+
22+
-------
23+
24+
##### Points to remember for Java Enum
25+
```
26+
Enum improves type safety
27+
Enum can be easily used in switch
28+
Enum can be traversed
29+
Enum can have fields, constructors and methods
30+
Enum may implement many interfaces but cannot extend any class because it internally extends Enum class
31+
```
32+
![](https://static.javatpoint.com/java/new/images/java-enum.png)
33+
34+
35+
Example
36+
```java
37+
class EnumExample1{
38+
//defining the enum inside the class
39+
public enum Season { WINTER, SPRING, SUMMER, FALL }
40+
//main method
41+
public static void main(String[] args) {
42+
//traversing the enum
43+
for (Season s : Season.values())
44+
System.out.println(s);
45+
}}
46+
```
47+
Output
48+
```
49+
WINTER
50+
SPRING
51+
SUMMER
52+
FALL
53+
```
54+
-----------
55+
56+
```java
57+
class EnumExample1{
58+
//defining enum within class
59+
public enum Season { WINTER, SPRING, SUMMER, FALL }
60+
//creating the main method
61+
public static void main(String[] args) {
62+
//printing all enum
63+
for (Season s : Season.values()){
64+
System.out.println(s);
65+
}
66+
System.out.println("Value of WINTER is: "+Season.valueOf("WINTER"));
67+
System.out.println("Index of WINTER is: "+Season.valueOf("WINTER").ordinal());
68+
System.out.println("Index of SUMMER is: "+Season.valueOf("SUMMER").ordinal());
69+
70+
}}
71+
```
72+
Output
73+
```
74+
WINTER
75+
SPRING
76+
SUMMER
77+
FALL
78+
Value of WINTER is: WINTER
79+
Index of WINTER is: 0
80+
Index of SUMMER is: 2
81+
```
82+
83+
------
84+
```
85+
Q- What is the purpose of the values() method in the enum?
86+
A- The Java compiler internally adds the values() method when it creates an enum. The values() method returns an array containing all the values of the enum.
87+
88+
Q- What is the purpose of the valueOf() method in the enum?
89+
A- The Java compiler internally adds the valueOf() method when it creates an enum. The valueOf() method returns the value of given constant enum.
90+
91+
Q- What is the purpose of the ordinal() method in the enum?
92+
A- The Java compiler internally adds the ordinal() method when it creates an enum. The ordinal() method returns the index of the enum value.
93+
94+
```
95+
96+
--------
97+
98+
```java
99+
class EnumExample4{
100+
enum Season{
101+
WINTER(5), SPRING(10), SUMMER(15), FALL(20);
102+
103+
private int value;
104+
private Season(int value){
105+
this.value=value;
106+
}
107+
}
108+
public static void main(String args[]){
109+
for (Season s : Season.values())
110+
System.out.println(s+" "+s.value);
111+
112+
}}
113+
```
114+
Output
115+
```
116+
WINTER 5
117+
SPRING 10
118+
SUMMER 15
119+
FALL 20
120+
```
121+
--------
122+

0 commit comments

Comments
 (0)