enum is introduced in JDK1.5 onward. When compared with old languages enum,the java enum is more powerful. Since,in java enum we are allowed to take instance members, constructors which may not be possible in old languages.Enums are mainly used for grouping similar kind of constants in single unit.Enums in java are declared with enum.It is good practice to declare constants with uppercase letters.
In other words enum is a group of named constants. It can also be used for defining user defined data types. Every constant inside enum is implicitly public,static and final by default.
Example:
enum Beer
{
ko,rc,kf,fo;
}
class test
{
public static void main(String [] args)
{
Beer b1 = Beer.fo;
System.out.println(b1);
}
}
we can take enum either inside the class or outside the class but not inside method. It leads to Compile time error like enum type must not be local.
- Because by default enum constants are public,static and final. So, in the method we can not declare any static members.
- If we are declaring the enum outside the class the allowed modifiers are public,<default>,and strictfp
- If we are declaring the enum within the class, the allowed modifiers are public,private,<default>,strictfp,and static
Every enum in java should be direct child class of java.lang.enum
As every enum is already extending java.lang.enum is no chance of extending any other enum. Hence inheritance concept not applicable to the enum. This is the reason abstract and final keywords are not applicable for enum.
Enum Vs Switch:
We can pass enumtype as an argument to switch staement.
Example:
enum Beer
{
kf,ko,fo;
}
class Test
{
public static void main(String arg[])
{
Beer b1 = Beer.ko;
switch(b1)
{
case kf : System.out.println("kf is not found");
break;
case ko : System.out.println("ko is childrens brand");
break;
case fo : System.out.println("Buy one get one free");
break;
default : System.out.println("The Other Brands are not good");
}
}
}
{
kf,ko,fo;
}
class Test
{
public static void main(String arg[])
{
Beer b1 = Beer.ko;
switch(b1)
{
case kf : System.out.println("kf is not found");
break;
case ko : System.out.println("ko is childrens brand");
break;
case fo : System.out.println("Buy one get one free");
break;
default : System.out.println("The Other Brands are not good");
}
}
}
Output:
Note:
It we are passing enum constants as the switch argument all the case labels should be valid enum constants otherwise we will get compile time error.
Values() method in enum:
Every enum implicitly contains values method to list all its' constants
Example:
enum Days
{
Sun,Mon,Tus,Wed;
}
class Demo
{
public static void main(String arg[])
{
Days [] d = Days.values();
for (Days d1 : d )
{
System.out.println(d1);
}
}
Output:Values() method in enum:
Every enum implicitly contains values method to list all its' constants
Example:
enum Days
{
Sun,Mon,Tus,Wed;
}
class Demo
{
public static void main(String arg[])
{
Days [] d = Days.values();
for (Days d1 : d )
{
System.out.println(d1);
}
}
Sun
Mon
Tus
Wed
No comments:
Post a Comment