-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumWithCustomizedVal.java
More file actions
54 lines (42 loc) · 1.28 KB
/
Copy pathEnumWithCustomizedVal.java
File metadata and controls
54 lines (42 loc) · 1.28 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
package Phase1_CoreLanguage.Enumerations;
/**By default enums have their own string values, we can also assign some custom values to enums.
* Consider below example for that.
* <p>
*
* enum Fruits
* {
* APPLE(“RED”), BANANA(“YELLOW”), GRAPES(“GREEN”);
* }
* <p>
*
*In above example we can see that the Fruits enum have three members i.e APPLE, BANANA and GRAPES with have their own
* different custom values RED, YELLOW and GREEN respectively.**/
// Java program to demonstrate how values can be assigned to enums.
enum FRUITS{
// This will call enum constructor with one String argument
APPLE("RED"),
BANANA("YELLOW"),
GREPS("GREEN");
// declaring private variable for getting values
String color;
//getter method
public String getColor() {
return color;
}
// enum constructor - cannot be public or protected
FRUITS(String color) {
this.color = color;
}
}
public class EnumWithCustomizedVal {
public static void main(String[] args) {
FRUITS[] fruits = FRUITS.values();
for (FRUITS fruits1 : fruits) {
System.out.println(fruits1 +" Color is "+ fruits1.getColor());
}
}
// OUTPUT
//APPLE Color is RED
//BANANA Color is YELLOW
//GREPS Color is GREEN
}