Click here to watch in Youtube :
https://www.youtube.com/watch?v=CKfZ45TtkbI&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
![]() |
Java Tutorial: Enum in java | Java enum |
public class EnumDemo1
{
public enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args)
{
/*
* The java compiler internally adds the values()
* method when it creates an enum. The values()
* method returns an array containing all the values
* of the enum.
*/
Day[] daysArray = Day.values();
for (Day day : daysArray)
{
System.out.println(day);
}
}
}
OutputSUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
EnumDemo2.javapublic class EnumDemo2
{
public enum Direction
{
NORTH, SOUTH, EAST, WEST
}
public static void main(String[] args)
{
/*
* The java compiler internally adds the values()
* method when it creates an enum. The values()
* method returns an array containing all the values
* of the enum.
*/
Direction[] directionsArray = Direction.values();
for (Direction direction : directionsArray)
{
System.out.println(direction);
}
}
}
OutputNORTH
SOUTH
EAST
WEST
EnumDemo3.javapublic class EnumDemo3
{
public enum Season
{
WINTER, SPRING, SUMMER, FALL
}
public static void main(String[] args)
{
/*
* The java compiler internally adds the values()
* method when it creates an enum. The values()
* method returns an array containing all the values
* of the enum.
*/
Season[] seasonArray = Season.values();
for (Season season : seasonArray)
{
System.out.println(season);
}
}
}
OutputWINTER
SPRING
SUMMER
FALL
Refer: https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html
Click the below link to download the code:
No comments:
Post a Comment