Enumeration (enum)
We can use enum to define a group of named constants
Example :
1) enum month
{
JAN,FEB,MAR, ......DEC;
}
By using enum we can define our own datatype
Enum concept introduced in 1.5v
When compared with old language enum, java's enum is more powerfull.
Internal Implementation of enum:-
1.Enum concept internally implemented by using class concept.
2.Every enum constant is a reference variable to enum type object.
3.Every enum constant is always public static final by default.
Declaration and usage of enum:-
Example :
enum Month
{
jan,feb,mar,...,dec;
}
class Test
{
public static void main(String[] args)
{
Month b=Month.dec;
}
}
enum Month
{
jan,feb,mar,...,dec;
}
class Test
{
public static void main(String[] args)
{
Month b=Month.dec;
}
}
We can declare enum either with in the class or outside of the class but not inside a method.
If we are trying to declare enum with in a method we will get compile time error.
Example :
Enum X class y
{ { {
Class y enum x public void m1()
} } {
} } enum x;
} }}}
Write write wrong
Enum X class y
{ { {
Class y enum x public void m1()
} } {
} } enum x;
} }}}
Write write wrong
Note :
enum type must not be local
If we declare enum outside the class the applicable modifiers are public,default, strict fp
If we declare enum with in a class the applicable modifiers are public,default,strictfp,private,protected,static.
Enums switch statement:-
Until 1.4v the allowed datatype for switch arguments are bye,short,char,int.
But from 1.5v onwards to above the corresponding wrapper classes Byte,char,integer,enum type also allowed
switch()
Enum Week
{
sun,mon,tues,wed,thur,fri,sat;
}
class test
{
public static void main(String[] args)
{
Week b1=Week.mon;
Switch(b1)
{
Case sun:
System.out.println( "today is holiday ");
break;
case mon:
System.out.println( "It is first day of week ");
break;
case tues:
System.out.println( "It is second day ");
break;
case wed:
System.out.println( "It is third day ")
break;
case thur:
System.out.println( "It is fourth day ")
break;
case fri:
System.out.println( "It is fifth day ")
break;
case sat:
System.out.println( "It is sixth day ")
break;
default:
System.out.println( "wrong input ");
}
}
}
O/P :
It is first day of week
Enum Week
{
sun,mon,tues,wed,thur,fri,sat;
}
class test
{
public static void main(String[] args)
{
Week b1=Week.mon;
Switch(b1)
{
Case sun:
System.out.println( "today is holiday ");
break;
case mon:
System.out.println( "It is first day of week ");
break;
case tues:
System.out.println( "It is second day ");
break;
case wed:
System.out.println( "It is third day ")
break;
case thur:
System.out.println( "It is fourth day ")
break;
case fri:
System.out.println( "It is fifth day ")
break;
case sat:
System.out.println( "It is sixth day ")
break;
default:
System.out.println( "wrong input ");
}
}
}
O/P :
It is first day of week
Enum vs Inheritance:-
Every enum in java is direct child class of java.lang.enum
As every enum is always extending java.lang.enum there is no chance of extending any other enum(because java wan 't provide support for multiple inheritance)
As every enum is always final implicitly we can 't create child enum for our enums.
Because of above reasons we can conclude inheritance concept is not applicable for enums explicitly.
But enum can implements any no. of interface at a time.
Example:
1) Enum x (2) enum x extends java.lang.enum
{
{
Enum y extends x //wrong
{
}
//wrong }
C.E:-
Cannot inherit form final x
Enum types not extensible
(1) Enum x (4)class x
{
{
//write
}
} //wrong
class y extends x enum y extends x
{
{
}
}
C.E1: cannot inherit from final x
C.E2: enumtypes are not extensible
(5)
Interface x
{
}
Enum y implements x
{
//write }
Java.lang.Enum:-
Every enum in java is always direct child class of java.lang.enum.class.
The power of enum is inheriting from this class only to our enum class.
It is an abstract class and direct child class of object class.
This class implements comparable and serializable interface. Hence every enum in java is byt default serializable and comparable.
Values() method:-
With in the enum the orders of constants is important we can specify its orders by using original value.
We can find ordinal value of enum constants by using ordinal method.
public int ordinal();
Ordinal value is zero-based
Enum class Constructors and speciality of java enum:-
When compared with old languages enum,Java enum is more powerful because in addition to constants we can take variables, methods, constructors etc.. Which may not possible in old language this extra facility is due to internal implementations of enum concept which is class based.
Inside enum we can declare main() methods and hence we can invoke enum class directly from command prompt.
Example:
Enum fish
{
STAR,GOLD,GUPPY,APOLLO,KILLER; mandatory
public static void main(String[] args)
{
System.out.println( " "ENUM MAIN METHODS ");
}
}
>javac fish.java
>java fish
O/P :- enum main method
Enum fish
{
STAR,GOLD,GUPPY,APOLLO,KILLER; mandatory
public static void main(String[] args)
{
System.out.println( " "ENUM MAIN METHODS ");
}
}
>javac fish.java
>java fish
O/P :- enum main method
Inside enum without having constants we can 't take any extra members, but empty enum is always valid
Example: Enum color
{
Public void m1()
{
//wrong
}
Enum color
{
//write
}
{
Public void m1()
{
//wrong
}
Enum color
{
//write
}
Enum class Constructors:-
With-in enum we can take constructors also.
Enum class constructors will be executed automatically at the time of enum class loading. Hence enum constants will be created at the time of class loading only. We can 't invoke enum Constructors explicitly
Example :
Enum Fruits
{
apple,mango,grapes;
Fruits()
{
System.out.println( "constructor ");
}
}
class Test
{
public static void main(String[] args)
{
Fruits b1=Fruits.apple;
System.out.println(b1);
}
}
o/p :
constructor
apple
Enum Fruits
{
apple,mango,grapes;
Fruits()
{
System.out.println( "constructor ");
}
}
class Test
{
public static void main(String[] args)
{
Fruits b1=Fruits.apple;
System.out.println(b1);
}
}
o/p :
constructor
apple
We can 't create objects of enum explicitly and hence we can 't call constructors directly.
Fruits b=new Fruits(); X or wrong
C.E:
Enumtypes may not be instantiated
Within the enum we can take instance and static methods but we can 't to take abstract methods.
Every enum constant represents an object hence what ever the methods we can apply on normal java object we can apply those on enum methods also.
Case(1) :
Package pack1;
package pack2
Public enum fish class Test1
{
{
STAR,GUPPY,APOLLO; p.s.v.m(String[] args)
}
{
System.out.println(STAR); //import static pack1.fish.STAR
//import static pack1.fish.*;
}
}
Package pack3;
package pack4;
Class Test2 class Test3
{ {
public static void main(String args[])
{
{
Fish f=fish.STAR(); fish f=fish.STAR; //import pack1.fish(m)
//import pack1.*;
System.out.println(f);
System.out.println(GUPPY);//import static pack1.fish.GUPPY or
// import static pack1.fish.*;
}
}
}
}
Import pack1.fish;
Or
Import pack1.*;
Case(2) :
Enum color
{
BLUE,RED;
{
Public void info()
{
System.out.println( "Dangerous color ");
}
}GREEN;
Public void info()
{
System.out.println( "universal color ");
}
}
Class Test
{
public static void main(String[] args)
{
Color[] c=color.value();
For(color c1=c)
{
C1.info();
}
}
}
o/p :-
universal color
Dangerous color
Universal color
Package pack1;
package pack2
Public enum fish class Test1
{
{
STAR,GUPPY,APOLLO; p.s.v.m(String[] args)
}
{
System.out.println(STAR); //import static pack1.fish.STAR
//import static pack1.fish.*;
}
}
Package pack3;
package pack4;
Class Test2 class Test3
{ {
public static void main(String args[])
{
{
Fish f=fish.STAR(); fish f=fish.STAR; //import pack1.fish(m)
//import pack1.*;
System.out.println(f);
System.out.println(GUPPY);//import static pack1.fish.GUPPY or
// import static pack1.fish.*;
}
}
}
}
Import pack1.fish;
Or
Import pack1.*;
Case(2) :
Enum color
{
BLUE,RED;
{
Public void info()
{
System.out.println( "Dangerous color ");
}
}GREEN;
Public void info()
{
System.out.println( "universal color ");
}
}
Class Test
{
public static void main(String[] args)
{
Color[] c=color.value();
For(color c1=c)
{
C1.info();
}
}
}
o/p :-
universal color
Dangerous color
Universal color
enumVsEnum VS Enumeration:-
enum:-
It is a keyword which can be used to define a group of named constants.
Enum:-
It is a class parent in java.lang package which acts as a base class for all java enums.
Enumeration:-
It is an interface present in java.utilpackage,which can be used for retrieving objects from collection one by one.