-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumConstructor.java
More file actions
47 lines (38 loc) · 1.19 KB
/
Copy pathEnumConstructor.java
File metadata and controls
47 lines (38 loc) · 1.19 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;
/**
* enum can contain constructor and it is executed separately for each enum constant at the time of enum class loading.
* We can’t create enum objects explicitly and hence we can’t invoke enum constructor directly.
* enum and methods :
* <p>
*
* enum can contain concrete methods only i.e. no any abstract method.
* filter_none
**/
// Java program to demonstrate that enums can have constructor
// and concrete methods.
// An enum (Note enum keyword inplace of class keyword)
enum Color {
RED, GREEN, BLUE;
// enum constructor called separately for each constant
private Color() {
System.out.println("Constructor called for : " +
this.toString());
}
// Only concrete (not abstract) methods allowed
public void colorInfo() {
System.out.println("Universal Color");
}
}
public class EnumConstructor {
public static void main(String[] args) {
Color c1 = Color.RED;
System.out.println(c1);
c1.colorInfo();
}
//OUTPUT
//Constructor called for : RED
//Constructor called for : GREEN
//Constructor called for : BLUE
//RED
//Universal Color
}