Sie sind auf Seite 1von 25

Enums

Enums are introduced in jdk1.5


Enums are Fixed no. of, Similiar kind of Constants.
Because they are constants, the names of an enum type's fields are in uppercase
letters.
Every enum constant is public, static & final by default.
Every enum constant can be represented as a valid java identifier.
Every enum constant should be unique.
All enum constants should be delimeted with comma (,)
Inside enum, not only enum constats are there,
e can keep attributes, methods, constructors, !"s, S!"s.
enum can be meber of .java file.
enum can be member of a class.
!or enums also .class files are "enerated.
Amon" all members of one .java file, only one member can be public.
#hichever member is public, then that name only can be used for savin" the
.java file.
If enum is public, then that .java file should be saved as enum name.
enum improves type safety
enum can be easily used in sitch
enum may implement many interfaces but cannot e$tend any class because it
internally e$tends Enum class
#ro$ram %&
enum ' (
%
Explanation&
enum A is a member of .java file.
!or enum also .class file is "enerated.
&ere A.class file is "enerated.
enum itself static by default.
Prepared by Sashi. 1
enum 'A' e$tends java.lan".Enum.
java.lan".Enum is an abstract class.
'his is the common base class of all (ava lan"ua"e enumeration types.
It is declared as follos)
public abstract class Enum extends Object implements Comparable, Serializable
'his clearly means that enums are comparable and seriali)able implicitl*.
All enum types in java are sin$letonb* default.
*o, you can compare enum t*pes usin$ +,,- operator also.
'his also means that all enums e$tends java.lan".Enum, so they can not extend an*
other class because java does not support multiple inheritance this ay. But, enums
can implement an* number of interfaces.
#ro$ram .&
public enum B +
%
class , +
%
interface - +
%
/interface E1 +
%
Explanation&
enums, classes, interfaces and annotations can become member of .java file.
&ere totally . .class files are "enerated.
Prepared by Sashi. 2
Examples of enum that is defined outside the class:
#ro$ram 0&
enum E +
C1, C2, C3, C4/
%
class ! +
public static 1oid main0*trin"12 ar"s3 +
*ystem.out.println04E.,1 ) 4 5 E.C13/
*ystem.out.println04E.,6 ) 4 5 E.C23/
%
%
2utput&
E.,1 ) ,1
E.,6 ) ,6
Explanation&
enum E contains . constants. All are uni7ue.

It is advisable to keep all the constants in 8ppercase.

All enum constants are public 9 static by default.

enum constants are static, Beco: of that only, e're accessin" the enum constants by
usin" class;name.
#ro$ram 3&
enum < +
CON1, CON2, CON3, CON4
%
class & +
public static 1oid main0*trin"12 ar"s3 +
< "1 = <.CON1/
< "6 = <.CON2/
*ystem.out.println04"1 ) 4 5 "13/
*ystem.out.println04"6.to*trin"03 ) 4 5 "6.to*trin"033/
%
%
2utput&
"1 ) ,>?1
"6.to*trin"03 ) ,>?6
Prepared by Sashi. 3
Explanation&
enum < has . constants.
&ere '/' is optional at the end of enum constant. Beco: enum '<' havin" only
constants 9 its not havin" any other members 0like constructors or methods3.
In main03,
"1 is a '<' type refrence hich is a < type and it can be initiali:ed ith '<' types
constant.
&ere '"1' is hich is pointin" ',>?1' constant.
"6 is a < type refrence hich is pointin" ',>?6' constant.
to*trin"03 method "ot overrided in enum. 'hat is hy if e print enum refrence, e're
"ettin" enum constant.
#ro$ram 4&
enum I +
TEST1, TEST2, TEST3, TEST4
%
class ( +
public static 1oid main0*trin"12 ar"s3 +
I t1 = I.TEST1/
*ystem.out.println04t1 ) 4 5 t13/
t1 = I.TEST2/
*ystem.out.println04t1.to*trin"03 ) 4 5 t1.to*trin"033/
%
%
2utput&
t1 ) 'E*'1
t1.to*trin"03 ) 'E*'6
#ro$ram 5&
enum @ +
T1, T2, T3, T4
%
class A +
public static 1oid main0*trin"12 ar"s3 +
@ k1 = @.T1/
*ystem.out.println04k1 ) 4 5 k13/
k1 = @.T2/
*ystem.out.println04k1 ) 4 5 k13/
k1 = @.T3/
*ystem.out.println04k1 ) 4 5 k13/
k1 = @.T4/
*ystem.out.println04k1 ) 4 5 k13/
%
%
Prepared by Sashi. 4
2utput&
k1 ) '1
k1 ) '6
k1 ) 'B
k1 ) '.
#ro$ram 6&
enum Conth +
JN, !E", #$, %$, #&, J'N, J'(, '), SE%T, OCT, NO*, +EC
%
class 8 +
public static 1oid main0*trin"12 ar"s3 +
Conth mB = Conth.#$/
*ystem.out.println04mB ) 4 5 mB3/
Conth m5 = Conth.#&/
*ystem.out.println04m5 ) 4 5 m53/
*ystem.out.println04Dth month ) 4 5 Conth.J'(3/
%
%
2utput&
mB ) CAE
m5 ) CAF
Dth month ) (8A
Explanation&
Enum is a fi$ed number of, similiar kind of ,onstatns.
&ere all the months are fi$ed 9 all are similiar kind.
'hat is hy these constatns are represented inside enum.
#ro$ram 7&
public class C1 +
enum En1 +
C1, C2, C3, C4/
%
%
class C6 +
public static 1oid main0*trin"12 ar"s3 +
C1.En1 e1 = C1.En1.C1/
C1.En1 e. = C1.En1.C4/
*ystem.out.println04e1 ) 4 5 e13/
*ystem.out.println04e. ) 4 5 e.3/
e. = C1.En1.C3/
*ystem.out.println04e..to*trin"03 ) 4 5 e..to*trin"033/
%
%
Prepared by Sashi. 5
2utput&
e1 ) ,1
e. ) ,.
e..to*trin"03 ) ,B
Explanation&
'his .java file contains 6 members, one enum and one class.

enums can become member of a class.
&ere enum 'En' is a member of class 'C'.
Eventou"h enum is a member of a class, !or this enum 'En1' also .class0C1GEn1.class3
file is "enerated.
'o access 'En1' enum in outside of 'C1' class, it should be accessed alon" ith
outer;classname.
&ere, C1.En1
,1 can be accessed outside like C1.En1.,1
Example of enum that is defined within the class:
#ro$ram 8&
public class C +
enum En +
C1, C2, C3, C4/
%
public static 1oid main0*trin"12 ar"s3 +
En e1 = En.C1/
En e6 = En.C2/
*ystem.out.println04e1 ) 4 5 e13/
*ystem.out.println04e6 ) 4 5 e63/
e6 = En.C4/
*ystem.out.println04e6.to*trin"03 ) 4 5 e6.to*trin"033/
%
%
2utput&
e1 ) ,1
e6 ) ,6
e6.to*trin"03 ) ,.
Prepared by Sashi. 6
Explanation&
enums can become member of a class.
&ere enum 'En' is a member of class 'C'.
Eventou"h enum is a member of a class, !or this enum 'En' also .class0CGEn.class3 file
is "enerated.
class 'C' contains 6 members. one is a enum, another one is a main03 method.
Example of enum with constructors:
Note : Constructor of enum type is private if you don't declare private
compiler internally have private constructor
#ro$ram %9&
enum *eason+
,-NTE$01H3,S'##E$06H3/
pri1ate int value/
*eason0int value3+
this.value=value/
%
%
Internal code generated by the compiler for the above example of enum type
final class *eason e$tends Enum
+
private *eason0*trin" s, int i, int j3
+
super0s, i3/
value = j/
%
public static final *eason ,-NTE$/
public static final *eason S'##E$/
private int value/
static
+
,-NTE$ = ne *eason04#I?'EE4, H, 1H3/
S'##E$ = ne *eason04*8CCEE4, 1, 6H3/
%/
%
Prepared by Sashi. 7
#ro$ram %%&
enum I +
C1, C2, C3/
I03 +
*ystem.out.println04enum constructor I0343/
%
%
public class # +
public static 1oid main0*trin"12 ar"s3 +
*ystem.out.println04Cain Be"in43/
I vB = I.C3/
*ystem.out.println04vB ) 4 5 vB3/
I v6 = I.C2/
*ystem.out.println04v6 ) 4 5 v63/
*ystem.out.println04I.,6 ) 4 5 I.C23/
%
%
2utput&
Cain Be"in
enum constructor I03
enum constructor I03
enum constructor I03
vB ) ,B
v6 ) ,6
I.,6 ) ,6
Explanation&
enum can contains constructors also.
Inside enum, e can keep contructors, methods, attributes, *IB's 9 IIB's.
&ere, enum constatns should end ith semicolon 0/3.
Beco: alon" ith enum constants, constructors also there.
If only enum constants are presents in 'enum' means, semicolon is optional.
If some other members also inside means, semicolon is mandatory.
#henever 'enum' is loadin" into memory, at that time each constant is loadin" into
memory, at that time, correspondin" constructor also e$ecutin".
Beco: enum constants are static, so all enum constants are loadin" into memory
henever particular 'enum' is loadin" into memory.
Prepared by Sashi. 8
&ere,
henever enum 'I' is loadin" into memory, all enum constants also loadin" into memory
one by one.
1st constant ',1' is loadin" into memory, that time no ar" constructor is e$ecutin"
automatically.
then constant ',6' is loadin" into memory, that time no ar" constructor is e$ecutin"
automatically.
then constant ',B' is loadin" into memory, that time no ar" constructor is e$ecutin"
automatically.
*o, totally, ,03 constructor is e$ecuted B times, beco: B constants are loaded into
memory.
>nce all enum constants are loaded in memory, that enum constants are available in the
memory, later e can use that constants in our pro"ram.
#ro$ram %.&
public class J +
enum A + KK static by default. 0i.e3 static enum A
CON101HH3, TEST06HH3/
A0int i3 +
*ystem.out.println04A0int i3 ) 4 5i3/
%
%
public static 1oid main0*trin"12 ar"s3 +
A a1 = A.CON1/
A a6 = A.TEST/
*ystem.out.println0a13/
*ystem.out.println0a13/
%
%
2utput&
A0int i3 ) 1HH
A0int i3 ) 6HH
,>?1
,>?1
Explanation&
henever class 'J' is loadin" into memory, all static members are loadin" into memory.
enum itself static by default.
&ere class 'J' contains 6 static members, one is enum 'A', 6nd one is main03 method.
*o both static members are loadin" into memory one by one.
#henever enum 'A' is loadin" into memory. all enum constants are loadin" into memory
one by one.
Prepared by Sashi. 9
hile loadin" each constant one by one, automatically correspodin" constructors also
e$ecuted automatically.
&ere,
1st ',>?1' constant is loadin" into memory hich consists '1HH' as ar"ument, so sin"le
ar"umented constructor is e$ecutin".
then ''E*'' constant is also loadin" into memory hich consists '6HH' as ar"ument, so
sin"le ar"umented constructor is e$ecutin".
*o, totally, A0int i3 constructor is e$ecuted 6 times, beco: 6 constants are loaded
into memory.
>nce all enum constants are loaded in memory, that enum constants are available in the
memory, later e can use that constants in our pro"ram.
#ro$ram %0&
public class F +
enum A +
CON1, CON201HH3, CON305H.5f3, CON404*A*&I43/
A03 +
*ystem.out.println04A0343/
%
A0int i3 +
*ystem.out.println04A0int3 ) 4 5i3/
%
A0float f3 +
*ystem.out.println04A0float3 ) 4 5f3/
%
A0*trin" s3 +
*ystem.out.println04A0*trin"3 ) 4 5s3/
%
%
public static 1oid main0*trin"12 ar"s3 +
A c1 = A.CON1/
A cB = A.CON2/
*ystem.out.println04c1 ) 4 5c13/
*ystem.out.println04cB ) 4 5cB3/
*ystem.out.println04A.,>?. ) 4 5 A.CON43/
%
%
2utput&
A03
A0int3 ) 1HH
A0float3 ) 5H.5
A0*trin"3 ) *A*&I
c1 ) ,>?1
cB ) ,>?6
A.,>?. ) ,>?.
Prepared by Sashi. 10
Explanation&
enum 'A' contains totally . types of constants.
!or every constant, there should be a correspondin" constructor, then only compilation
success.
If any constructor is missin", then compile time error.
#ro$ram %3&
public class L +
public enum B +
CON101H3, CON205H3, CON301HH3/
int i/
B0int i3 +
*ystem.out.println04B0int343/
this.i = i/
%
%
public static void main0*trin"12 ar"s3 +
B b1 = B.CON1/
B b6 = B.CON2/
*ystem.out.println04b1 ) 4 5 b13/
*ystem.out.println04b6 ) 4 5 b63/
*ystem.out.println04b1.i ) 4 5 b1.i3/
*ystem.out.println04b6.i ) 4 5 b6.i3/
*ystem.out.println04MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM43/
*ystem.out.println048sin" values03 method ith Enhanced for loop43/
*ystem.out.println04MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM43/
for 0B b ) B..alues033 +
*ystem.out.println04b.i ) 4 5 b.i3/
%
%
%
2utput&
B0int3
B0int3
B0int3
b1 ) ,>?1
b6 ) ,>?6
b1.i ) 1H
b6.i ) 5H
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
8sin" values03 method ith Enhanced for loop
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
b.i ) 1H
b.i ) 5H
b.i ) 1HH
Prepared by Sashi. 11
:ia$ram&
Explanation&
Inside enum 'B', B constants 9 one attribute 'i' is available.
!or each enum constant, one copy of attribute 'i' is available.
Each enum constant contains one 'i'attribute.
#henever enum constants are loadin" into memory, constructors are e$ecuted.
Alon" ith that all non static members 0attributes 9 methods3 are loadin" into memory.
hile loadin" ',>?1', 'i' is loadin" into memory and becomin" part of ',>?1'
hile loadin" ',>?6', 'i' is loadin" into memory and becomin" part of ',>?6'
hile loadin" ',>?B', 'i' is loadin" into memory and becomin" part of ',>?B'
In main03 method,
'b1' is pointin" ',>?1', inside ',>?1' there is 'i' attribute, that 'i' value is '1H'.
'b6' is pointin" ',>?6', inside ',>?6' there is 'i' attribute, that 'i' value is '5H'.
#ro$ram %4&
public class L1 +
enum , +
T1, T2, T3, T4, T//
1oid test03 +
*ystem.out.println04test0343/
%
%
public static 1oid main0*trin"12 ar"s3 +
, t1 = ,.T1/
, t6 = ,.T2/
, t5 = ,.T//
t1.test03/
t6.test03/
t5.test03/
,.T4.test03/
%
%
Prepared by Sashi. 12
2utput&
test03
test03
test03
test03
:ia$ram&
Explanation&
Inside enum ',', 5 constants 9 1 method is there.
!or each constant, one copy of test03 method is there.
#henever one enum constant is loadin" into memory, all non constant members
0attributes 9 methods3 loadin" into memory 9 becomin" part of that 'enum constant'.
#henever object is created, all non static members are loadin" into memory 9 becomin"
part of that object.
*imilarly,
#henever one enum constant is loadin" into memory, all non static members are loadin"
into memory 9 becomin" part of that enum constant.
Prepared by Sashi. 13
#ro$ram %5&
public class L6 +
enum ,1 +
T101H3, T205.Nf3, T304*ashi43, T406H, 6.1f, 4@eerthana43/
pri1ate int id/
pri1ate float hei"ht/
pri1ate *trin" name/
,10int id3 +
*ystem.out.println04,10int343/
this.id = id/
%
,10float hei"ht3 +
*ystem.out.println04,10float343/
this.hei"ht = hei"ht/
%
,10*trin" name3 +
*ystem.out.println04,10*trin"343/
this.name = name/
%
,10int id, float hei"ht, *trin" name3 +
*ystem.out.println04,10int, float, *trin"343/
this.id = id/
this.hei"ht = hei"ht/
this.name = name/
%
public int "etId03 +
*ystem.out.print04"etId03 ) 43/
return id/
%
public float "et&ei"ht03 +
*ystem.out.print04"et&ei"ht03 ) 43/
return hei"ht/
%
public *trin" "et?ame03 +
*ystem.out.print04"et?ame03 ) 43/
return name/
%
O>verride
public *trin" to*trin"03 +
return 4,1 1id=4 5 id 5 4, hei"ht=4 5 hei"ht 5 4, name=4 5 name 5 424/
%
%
Prepared by Sashi. 14
public static 1oid main0*trin"12 ar"s3 +
*ystem.out.println041. =========================43/
,1 t1 = ,1.T1/
,1 t6 = ,1.T2/
,1 tB = ,1.T3/
,1 t. = ,1.T4/
*ystem.out.println046. =========================43/
*ystem.out.println0t13/
*ystem.out.println0t63/
*ystem.out.println0tB3/
*ystem.out.println0t..to*trin"033/
*ystem.out.println04MMMMMMMMMMMMMMMMMMM43/
*ystem.out.println0t1.id3/
*ystem.out.println0t1.name3/
*ystem.out.println04MMMMMMMMMMMMMMMMMMM43/
*ystem.out.println0t6.hei"ht3/
*ystem.out.println04MMMMMMMMMMMMMMMMMMM43/
*ystem.out.println0tB.name3/
*ystem.out.println04MMMMMMMMMMMMMMMMMMM43/
*ystem.out.println0t1."etId033/
*ystem.out.println0t1."et&ei"ht033/
*ystem.out.println04MMMMMMMMMMMMMMMMMMM43/
*ystem.out.println0t6."et&ei"ht033/
*ystem.out.println04MMMMMMMMMMMMMMMMMMM43/
*ystem.out.println0tB."et?ame033/
*ystem.out.println04MMMMMMMMMMMMMMMMMMM43/
*ystem.out.println0t.."etId033/
*ystem.out.println0t.."et?ame033/
%
%
Prepared by Sashi. 15
2utput&
1. =========================
,10int3
,10float3
,10*trin"3
,10int, float, *trin"3
6. =========================
,1 1id=1H, hei"ht=H.H, name=null2
,1 1id=H, hei"ht=5.N, name=null2
,1 1id=H, hei"ht=H.H, name=*ashi2
,1 1id=6H, hei"ht=6.1, name=@eerthana2
MMMMMMMMMMMMMMMMMMM
1H
null
MMMMMMMMMMMMMMMMMMM
5.N
MMMMMMMMMMMMMMMMMMM
*ashi
MMMMMMMMMMMMMMMMMMM
"etId03 ) 1H
"et&ei"ht03 ) H.H
MMMMMMMMMMMMMMMMMMM
"et&ei"ht03 ) 5.N
MMMMMMMMMMMMMMMMMMM
"et?ame03 ) *ashi
MMMMMMMMMMMMMMMMMMM
"etId03 ) 6H
"et?ame03 ) @eerthana
:ia$ram&
Prepared by Sashi. 16
Inbuilt methods of Enum:
1. values( method:
#ro$ram %6&
public class IaluesCethod +
enum -ays +
S'N, #ON, T'E, ,E+, T0', !$-, ST
%
public static 1oid main0*trin"12 ar"s3 +
-ays all-ays12 = -ays..alues03/
*ystem.out.println04Iteratin" usin" ?ormal for loop43/
for 0int i=H/ iPall-ays.len"th/ i553 +
*ystem.out.println0all-ays1i23/
%
*ystem.out.println04Iteratin" usin" Enhanced for loop43/
for 0-ays day ) all-ays3 +
*ystem.out.println0day3/
%
%
%
2utput&
Iteratin" usin" ?ormal for loop
*8?
C>?
'8E
#E-
'&8
!EI
*A'
Iteratin" usin" Enhanced for loop
*8?
C>?
'8E
#E-
'&8
!EI
*A'
Explanation&
values03 method returns all the enum constants into an array.
'o "et all the constants use values03 method.
Prepared by Sashi. 17
.. 2rdinal() ;ethod&
#ro$ram %7&
public class >rdinalCethod +
enum -irections +
EST, ,EST, NO$T0, SO'T0/
%
public static 1oid main0*trin"12 ar"s3 +
-irections d1 = -irections.EST/
-irections d6 = -irections.NO$T0/
*ystem.out.println04d1 ) 4 5 d13/
*ystem.out.println04d6 ) 4 5 d63/
*ystem.out.println04MMMMMMMMMMMM43/
*ystem.out.println04d1.ordinal03 ) 4 5 d1.ordinal033/
*ystem.out.println04d6.ordinal03 ) 4 5 d6.ordinal033/
*ystem.out.println04MMMMMMMMMMMM43/
*ystem.out.println04-irections.*>8'&.ordinal03 ) 4 5
-irections.SO'T0.ordinal033/
*ystem.out.println04-irections.#E*'.ordinal03 ) 4 5
-irections.,EST.ordinal033/
%
%
2utput&
d1 ) EA*'
d6 ) ?>E'&
MMMMMMMMMMMM
d1.ordinal03 ) H
d6.ordinal03 ) 6
MMMMMMMMMMMM
-irections.*>8'&.ordinal03 ) B
-irections.#E*'.ordinal03 ) 1
Explanation&
enums are storin" from Hth inde$ onards.
ordinal03 method returns the order of the particular enum constant.
It is used for findin" out order of the particular enum constant.
ordinal03 method return type is 'int'.
enum constants are startin" ith inde$ H.
Prepared by Sashi. 18
0. 1alue2f(Strin$) ;ethod&
#ro$ram %8&
public class Ialue>fCethod +
enum <ender +
#(E, !E#(E/
%
public static void main0*trin"12 ar"s3 +
<ender "1 = <ender.#(E/
<ender "6 = <ender.!E#(E/
*ystem.out.println0"13/
*ystem.out.println0"63/
*ystem.out.println04MMMMMMMMMMMMM43/
*ystem.out.println0<ender..alueO104CAAE433/
KK Accessin" in a *tatic ay. 'his is absolutely correct
*ystem.out.println0"1..alueO104!ECAAE433/
KK Accessin" in a ?on *tatic ay. 'his is absolutely correct
*ystem.out.println0<ender..alueO104&ello433/
KK &ello is not available. *o it thros 4Ille"alAr"umentE$ception4.
%
%
2utput&
CAAE
!ECAAE
MMMMMMMMMMMMM
CAAE
!ECAAE
E$ception in thread 4main4 java.lan".Ille"alAr"umentE$ception)
?o enum const class methodsInEnum.Ialue>fCethodG<ender.&ello
#ro$ram .9&
class Ialue>fCethod1+
public enum *eason +

,-NTE$, S%$-N), S'##E$, !((
%
public static void main0*trin"12 ar"s3 +
for 0*eason s ) *eason..alues033
*ystem.out.println04s ) 4 5 s3/
%
%
2utput&
s ) #I?'EE
s ) *QEI?<
s ) *8CCEE
s ) !AAA
Prepared by Sashi. 19
Inbuilt Enums:
#ro$ram .%&
public class C"r1 +
public static 1oid main0*trin"12 ar"s3 +
KK <ettin" all the 'hread states into an array.
'hread.*tate states12 = 'hread.*tate..alues03/
*ystem.out.println04'hread *tates are)43/
for 0'hread.*tate state ) states3 +
*ystem.out.println0state3/
%
%
%
2utput&
<hread States are&
?E#
E8??ABAE
BA>,@E-
#AI'I?<
'ICE-;#AI'I?<
'EECI?A'E-
!ethod overriding "oncept:
#ro$ram ..&
public class L. +
enum ! +
T1,
T2 +
1oid method03 +
*ystem.out.println04Inner43/
%
%, T3/
1oid method03 +
*ystem.out.println04<eneral43/
return/
%
%
public static 1oid main0*trin"12 ar"s3 +
! t1 = !.T1/
! t6 = !.T2/
! tB = !.T3/
*ystem.out.println0t13/
*ystem.out.println0t63/
*ystem.out.println0tB3/
*ystem.out.println04MMMMMMMMMMMMMMMMMMMM43/
t1.method03/
t6.method03/
tB.method03/
%
%
Prepared by Sashi. 20
2utput&
'1
'6
'B
MMMMMMMMMMMMMMMMMMMM
<eneral
Inner
<eneral
:ia$ram&
Explanation&
enum '!' contains B constants.

'1, 9 'B contains one method hich is shon belo,

void method03 +
*ystem.out.println04<eneral43/
return/
%
'6 constant contains , specific class body hich is shon belo,
void method03 +
*ystem.out.println04Inner43/
%
In this class body, 8 can keep specific thin"s for this particular enum constant.
#ro$ram .0&
enum -irection +
Prepared by Sashi. 21
KK Enum types
EST0H3 +
O>verride
public 1oid shout03 +
*ystem.out.println04-irection is East RRR43/
%
%,
,EST01SH3 +
O>verride
public 1oid shout03 +
*ystem.out.println04-irection is #est RRR43/
%
%,
NO$T00NH3 +
O>verride
public 1oid shout03 +
*ystem.out.println04-irection is ?orth RRR43/
%
%,
SO'T006DH3 +
O>verride
public 1oid shout03 +
*ystem.out.println04-irection is *outh RRR43/
%
%/
KK ,onstructor
pri1ate -irection0final int an"le3 +
*ystem.out.println04,onstructor ) -irection0int3 ) an"le = 4 5 an"le3/
this.an"le = an"le/
%
KK Internal state
pri1ate int an"le/
public int "etAn"le03 +
return an"le/
%
KK Abstract method hich need to be implemented
public abstract 1oid shout03/
%
class C"r +
public static 1oid main0*trin"12 ar"s3 +
*ystem.out.println04Cain Be"in43/
-irection d1 = -irection.EST/
*ystem.out.println04d1 ) 4 5 d13/
*ystem.out.println04d1."etAn"le03 ) 4 5 d1."etAn"le033/
d1.shout03/
Prepared by Sashi. 22
*ystem.out.println04MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM43/
-irection d6 = -irection.SO'T0/
*ystem.out.println04d6 ) 4 5 d63/
*ystem.out.println04d6."etAn"le03 ) 4 5 d6."etAn"le033/
d6.shout03/
%
%
2utput&
Cain Be"in
,onstructor ) -irection0int3 ) an"le = H
,onstructor ) -irection0int3 ) an"le = 1SH
,onstructor ) -irection0int3 ) an"le = NH
,onstructor ) -irection0int3 ) an"le = 6DH
d1 ) EA*'
d1."etAn"le03 ) H
-irection is East RRR
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
d6 ) *>8'&
d6."etAn"le03 ) 6DH
-irection is *outh RRR
#hy we re$uire arguments to the enum constant%
,onsider, if every enum constant havin" some value, that value is varyin" from one
constant to another constant.
'hat varyin" data e can supply as an ar"ument.
#ro$ram .0&
public class C"r6 +
enum Conth +
Prepared by Sashi. 23
JN0B13, !E"06S3, #$0B13, %$0BH3, #&0B13,
J'N0B13, ')0B13, SE%T0BH3, OCT0B13, NO*0BH3, +EC0B13/
int days/
Conth0int days3 +
*ystem.out.println04Conth0int3 ) 4 5 days3/
this.days = days/
%
public int "et-ays03 +
return days/
%
%
public static void main0*trin"12 ar"s3 +
Conth m1 = Conth.!E"/
*ystem.out.println04!eb. total no. of -ays ) 4 5 m1."et-ays033/
*ystem.out.println04>ct. total no. of -ays ) 4 5 Conth.OCT3/
%
%
2utput&
Cain Be"in
Conth0int3 ) B1
Conth0int3 ) 6S
Conth0int3 ) B1
Conth0int3 ) BH
Conth0int3 ) B1
Conth0int3 ) B1
Conth0int3 ) B1
Conth0int3 ) BH
Conth0int3 ) B1
Conth0int3 ) BH
Conth0int3 ) B1
!eb. total no. of -ays ) 6S
>ct. total no. of -ays ) >,'
Explanation&
&ere,
days are common to each enum constant 0i.e3 each month. 'hat days are varyin" from one
month to another month.
'hose varyin" days, e're supplyin" as an ar"ument.
Collectin$ ke* notes about enum&
enums are implicitly final subclasses of java.lan".Enum
if an enum is a member of a class, itTs implicitly static
ne can never be used ith an enum, even ithin the enum type itself
name and value>f simply use the te$t of the enum constants, hile to*trin" may be
Prepared by Sashi. 24
overridden to provide any content, if desired
for enum constants, e7uals and == amount to the same thin", and can be used
interchan"eably
enum constants are implicitly public static final
the order of appearance of enum constants is called their Unatural orderV, and
defines the order used by other items as ell ) compare'o, iteration order of
values , Enum*et, Enum*et.ran"e.
,onstructors for an enum type should be declared as private. 'he compiler allos
non private declares for constructors.
*ince these enumeration instances are all effectively sin"letons, they can be
compared for e7uality usin" identity 0U==V3.
Fou can use Enum in (ava inside *itch statement like int or char primitive data
type
%. Can we create the instance of enum b* new ke*word=
?o, because it contains private constructors only.
6. ,an e have abstract method in enumW
Fes, ofcourseR e can have abstract methods and can provide the implementation of these
methods.
3. Can we extend enum t*pes=
?>, you can not. Enum t2pes are 1inal b2 de1ault and hence can not be e$tended. Fet,
you are free to implement any number of interfaces as you like.
Prepared by Sashi. 25

Das könnte Ihnen auch gefallen