enum
It defines a list of named constants.This is interdused in 1.5 version.
Java enum is powerful than c,c++ enums because java enum may contain methods ,constructors and instance variables in addition to constants.But c,c++ enum contain only constants.
Example:
enum Month
{
Jan,Feb,Mar,Apr…….;
}
Example:
enum Beer
{
KF,RC,H5,H2,…….;
}
No variables in enum.At the end ( ; ) no need to specify (optional)
Use:we can reduce the number of bugs by using enumeration because We are providing a set of predefined values for the variables.
Enum Example code:
enum Month
{
Jan,Feb,Mar,Apr;
}
class Sample
{
public static void main(String arg[])
{
Month m=Month.Mar;
System.out.println(m); // Mar
}
}
- We can keep enum outside the class or inside the class.If we are declaring enum outside the class,the allowed modifiers are public ,<default>.we can declare enum with in a class the allowed modifiers are public ,<default>,protected,private.
- We never allowed to declare enum inside a method,violation leads to compile time error saying enum type must not be local.
- Enum types is allowed to used as argument for the switch statement.
Enum Beer
{
KF,RC,H5;
}
class Sample
{
public static void main(String arg[])
{
Beer b= Beer.KF;
Switch(b)
{
case KF:
System.out.println(“too Bitter”);
break;
case RC:
System.out.println(“too Hot”);
break;
case H5:
System.out.println(“too strong”);
break;
}
}
}
until 1.4 version the allowed arguments for the switch statement are byte,short,int,char.But 1.5 version onwords in addition to these arguments Byte,Short,Character,Integer and enum also.
Values() method:
Enum contains a predefined method values to list the constants available in that enum.
Example :
enum Month
{
Jan,Feb,Mar,Apr;
}
class Client
{
public static void main(String arg[])
{
Month[] m=Month.values();
for(Month m1:m)
{
System.out.println(m1); // Jan,Feb,Mar,Apr
}
}
enum never participated in inheritance hierarchy because every enum class implicitly extends java.lang .enum .Thats why we never allowed to create a child classfor the enum.As Month enum already extends java.lang.enum hence it never allowed to extend any other.
Enum Month extends Some ---> not possible
If we are writing any enum it is extending java.lang.enum class.Hence it never extends any thing else.So inheritance concept not applicable for enums.
Example:
Enum Beer
{
KF(65),RC(50),H5(100),KO;
int price;
Beer(int price)
{
this.price=price;
}
public int getPrice()
{
return price;
}
Beer()
{
this.price=100;
}
}
class Sample
{
public static void main(String arg[])
{
Beer b=Beer.KF;
System.out.println(b); //KF
System.out.println(Beer.KF.ordinal());
System.out.println(b.price); //65
System.out.println(b.getPrice()); //65
Beer[] s=Beer.values();
for(Beer s1:s)
{
System.out.println(s1+”……….”+s1.getPrice());
}
}
}
• When ever enum is loaded into the memory, all the enum constants will be assigned and JVM calls Constructor automatically. The programmer is not allowed to call enum constructor explicitly.
• The Constructors of enum can be overloaded if the enum contain both enum constants, instance variables, the first line should be enum constants and ends with semicolon.
i.e.
enum Beer
{
KF, RC //invalid ; missing
int price;
}
enum Beer
{
int price; //invalid because the 1st stmt should be enum constants.
KF, RC;
}
enum Beer
{
KF, RC;
int price; //valid
}
Ordinal Value
• The position of enum constant is important and indicated by their ordinal values. We can ordinal value for any enum constant by the fallowing method.
Public final int ordinal();
Ordinal value starts from zero(0).
Enum Month
{
JAN,FEB;
public static void main(String a[])
{
Month[] m=Month.values();
for(Month m1:m)
System.out.println(m1);//JAN,FEB
}
}//Save:Month.java, Run:Java Month
- So just like a normal class, we can run enum also.
- So we can compare the constants declared in enum. Otherthan ‘= =’operators, remaining <=, >=,<,> will gave CTE.
- Duplicate constants can’t be declared. So, constants are unique.
enum Month
{
Jan,Feb,Jan; //invalid,CTE:Jan already defined
}
But
enum Month
{
Jan,Feb,JAN; //valid, due to case sensitive jan!=JAN
}
2 comments:
Good tutorial.
Ajay Rathore.
Impetus Indore(M.P.)
9981795923
My Favourite Core Java Tutorial.
Srinivas
Post a Comment