Saturday, 10 June 2017

Java Tutorial: Enum in java[How to define a constructor and method in enum - Season] ~ foundjava


Click here to watch in Youtube :
https://www.youtube.com/watch?v=iTBBU3XRu3g&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Enum in java[How to define a constructor and method in enum - Season] 
Season.java
public enum Season
{
    WINTER(5), SPRING(10), SUMMER(15), FALL(20);

    private int seasonNumber;

    private Season(int seasonNumber)
    {
        this.seasonNumber = seasonNumber;
    }

    public int getSeasonNumber()
    {
        return seasonNumber;
    }
}
EnumDemo.java
public class EnumDemo
{

    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 + ", SeasonNumber =  "
                                    + season.getSeasonNumber());
        }

    }
}
Output
WINTER, SeasonNumber =  5
SPRING, SeasonNumber =  10
SUMMER, SeasonNumber =  15
FALL, SeasonNumber =  20

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment