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] |
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);
}
}
}
OutputMONDAY 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:
No comments:
Post a Comment