-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenumerations.java
More file actions
42 lines (34 loc) · 1.18 KB
/
enumerations.java
File metadata and controls
42 lines (34 loc) · 1.18 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
// Java Enumerations are class types.
// Although you dont have to instantiate an enum using new.
enum Apple{
redhat, whitehat,ethical // no semicolon required here
}
public class enumerations {
public static void main(String args[])
{
Apple ap;
ap=Apple.ethical;
System.out.println(ap);
ap=Apple.redhat;
System.out.println(ap);
ap=Apple.whitehat;
System.out.println(ap);
// comparing two enums
if(ap==Apple.ethical) // since condition is false, therefore "Inside if" will not get printed in console.
{
System.out.println("Inside if"); //body
}
// Use of enum to control a switch statement
switch(ap)
{
case redhat:
System.out.println("Redhat from switch");
break;
case ethical:
System.out.println("ethical from switch");
case whitehat:
System.out.println("Whitehat from switch"); // since the last and recent value of ap=Apple.whitehat, therefore this will be printed.
break;
}
}
}