Saturday, 10 June 2017

Java Tutorial: Enum in java | Java enum ~ foundjava


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 
EnumDemo1.java
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);
        }

    }
}
Output
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
EnumDemo2.java
public 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);
        }

    }
}
Output
NORTH
SOUTH
EAST
WEST
EnumDemo3.java
public 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);
        }

    }
}
Output
WINTER
SPRING
SUMMER
FALL
Refer: 
https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment