Friday 5 May 2017

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


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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4ee0c57df034098ab5ebc06d5a6296fa5950097a/BasicJava/EnumDemo_method_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