Friday, 5 May 2017

Java Tutorial: Enum in java[How to define a constructor in enum]


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

Click the below Image to Enlarge
Java Tutorial: Enum in java[How to define a constructor in enum] 
EnumDemo.java
public class EnumDemo
{

    private enum Day
    {
        MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6),
                                                                    SUNDAY(7);

        private int whichDay;

        /*
         * Constructor of enum type is private. If you don't
         * declare private compiler internally creates
         * private constructor.
         */
        private Day(int whichDay)
        {
            this.whichDay = whichDay;
        }

    }

    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 + " is which day? " + day.whichDay);
        }

    }
}
Output
MONDAY is which day? 1
TUESDAY is which day? 2
WEDNESDAY is which day? 3
THURSDAY is which day? 4
FRIDAY is which day? 5
SATURDAY is which day? 6
SUNDAY is which day? 7
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/EnumDemo_cons_App.zip?attredirects=0&d=1

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

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