-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumMethods.java
More file actions
47 lines (39 loc) · 1.34 KB
/
Copy pathEnumMethods.java
File metadata and controls
47 lines (39 loc) · 1.34 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
package Phase1_CoreLanguage.Enumerations;
/**values(), ordinal() and valueOf() methods :
These methods are present inside java.lang.Enum.
values() method can be used to return all values present inside enum.
Order is important in enums.By using ordinal() method, each enum constant index can be found, just like array index.
valueOf() method returns the enum constant of the specified string value, if exists.
filter_none
edit
play_arrow
brightness_4
// Java program to demonstrate working of values(),
// ordinal() and valueOf()
enum Color **/
public class EnumMethods {
public static void main(String[] args)
{
// Calling values()
MainInsideEnum arr[] = MainInsideEnum.values();
// enum with loop
for (MainInsideEnum col : arr)
{
// Calling ordinal() to find index
// of color.
System.out.println(col + " at index "
+ col.ordinal());
}
// Using valueOf(). Returns an object of
// Color with given constant.
// Uncommenting second line causes exception
// IllegalArgumentException
System.out.println(MainInsideEnum.valueOf("RED"));
// System.out.println(Color.valueOf("WHITE"));
// OUTPUT
//RED at index 0
//GREEN at index 1
//BLUE at index 2
//RED
}
}