Click here to watch in Youtube :
https://www.youtube.com/watch?v=F4V9gr8wd0Q&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
![]() |
Java Tutorial: Enum in java[Define Enum inside or outside of the class] |
public class EnumInsideDemo
{
/*
* Enum is Defined inside of the class.
*/
private 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
EnumOutsideDemo.java /*
* Enum is Defined outside of the class.
*/
enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public class EnumOutsideDemo
{
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
Click the below link to download the code:
No comments:
Post a Comment