-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColors.java
More file actions
44 lines (37 loc) · 1.1 KB
/
Colors.java
File metadata and controls
44 lines (37 loc) · 1.1 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
/*
public class colors {
enum Color {
RED,
ORANGE,
GREEN;
}
public static void main (String[] args){
Color[] trafficLights = Color.values();
System.out.println(trafficLights[0] + " means stop");
System.out.println(trafficLights[1] + " means get ready");
System.out.println(trafficLights[2] + " means GO");
}
} */
public class Colors {
enum Color { //here we are declaring an enum type called Color with a string representation of its meaning
RED("stop"),
ORANGE("get ready"),
GREEN("GO");
//meaning is private(can't be accessed outside this class)
//its also final thus immutable
private final String meaning;
//constructor for the enum color and intialising its string meaning
Color(String meaning) {
this.meaning = meaning;
}
//A method that returns the string meaning
public String getMeaning() {
return meaning;
}
}
public static void main(String[] args) {
for (Color color : Color.values()) {
System.out.println(color + " means " + color.getMeaning());
}
}
}