Thursday 4 May 2017

Java Tutorial: Enum in java | Java enum


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:
https://sites.google.com/site/ramj2eev1/home/javabasics/EnumDemo_Intro_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/EnumDemo_Intro_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2f1fa585daa61fc8fa0957d6f04b940eddbcf7ec/BasicJava/EnumDemo_Intro_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment