0% found this document useful (0 votes)
69 views9 pages

Enum

Uploaded by

vikram.singh6822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
69 views9 pages

Enum

Uploaded by

vikram.singh6822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
Core java NAGOOR BABU ENUM * Weccan use enum to define a group of named constants. Example 1; enum Month { JAN,FEB,MAR,DEC; } Example 2: enum Beer { KF,KO,RC,FO; } ‘© Enum concept introduced in 1.5 versions. ‘* When compared with old languages enum java’s enum is more powerful. ‘* By using enum we can define our own data types which are also come enumerated data types. Internal implementation of enum: ‘* Internally enum’s are implemented by using class concept. Every enum constant is a reference variable to that enum type object. * Every enum constant is implicitly public static final always. Example 3: enum Beer final class Beer extends java.lang.Enum{ { public static final Beer KF=new Beer(); KF,KO; public static final Beer KO=new Beer(); i } Diagram: of enu enum Beer { KF,KO,RC,FO;//here semicolon is optional. t class Test { public static void main(String args{IM{ DURGA SOFT 440 Core java NAGOOR BABU Beer bi=Beer KF; System.out.printin(b1); num>java Test Note: Every enum constant internally static hence we can access by using “enum name”. Enum vs switch statement: ‘Until 1.4 versions the allowed types for the switch statement are byte, short, char int. But from 1.5 version onwards in addition to this the corresponding wrapper classes and enum type also allowed. That is from 1.5 version onwards we can use enum type as argument to switch statement. Diagram: wyte Byte Fractal short Short ) i roger |* SS char Character Lav + =m Example: enum Beer { KF,KO,RC,FO; } class Test{ public static void main(String args{]}{ Beer b1=Beer.RC; switch(b1}{ case KF: System.out.printin("it is childrens brand"); break; case KO: System.out printin("it is too lite"); break; case RC: aa DURGA SOFT 444 Core java NAGOOR BABU ‘System.out.printin("it is too hot"); break; case FO: ‘System.out.printin("buy one get one’ break; default: ‘System.out.printin("“other brands are not good"); } y ‘Qutput: D:\Enum>java Test Itis too hot ‘* If we are passing enum type as argument to switch statement then every case label should be a valid enum constant otherwise we will get compile time error. Example: enum Beer { KF,KO,RC,FO; } class Test{ public static void main(String args{]M Beer b1=Beer.RC; switch(b1}{ case KF: case RC: case KALYANI: W Output: ‘Compi D:\Enumpjavac Test.java Test,java:11: unqualified enumeration constant name required case KALYAN! ‘* We can declare enum either outside the class or within the class but not inside a method. if we declare enum outside the class the allowed modifiers are: 1) public 2) default 3) strietfp. * Ifwe declare enum inside a class then the allowed modifiers are: e error. a2, DURGA SOFT 442 Core java NAGOOR BABU 1) public private 2) default + protected 3) strictfp static class x enum x | classx | 0 t public void methodOne(){ classy | enumY 0 0 5 Boy -ompile time error. (vatidy Fyatigy |} fos\EnumpjavacX.java Javad: enum types must not be local num X Enum vs inheritance: © Every enum in java is the direct child class of java.lang.Enum class hence it is not possible to extends any other enum. * Every enum is implicitly final hence we can’t create child enum. © Because of above reasons we can conclude inheritance concept is not applicable for enum’s explicitly. But enum can implement any no. Of interfaces simultaneously. Exampl ee : ‘enum X extends Enum fon enum Yextendsx | ‘enum ¥ extends X 0 0 id) (invalid) {invalid) enumXx 0 class V extends X 0 output: interface x compile time error. 0 D:\Enum>javac ¥ java ‘enum ¥ implements X Y.java:3:cannotinherit from finalx | {} class V extends X aaa ¥.java:3: enum types are not extensible class Y extends X {invalid Java.lang.Enum: Every enum in java is the direct child class of java.lang.Enum. The power of enum is inhé ing from this class only. aa3 DURGA SOFT 443 Core java NAGOOR BABU '* It is abstract class and it is direct child class of “Object class” it implements Serializable and Comparable. values() method: Every enum implicitly contains a static values() method to list all constants of enum. Example: Beer{] b=Beer.values(); ‘ordinal{) method: Within enum the order of constants is important we can specify by its ordinal value. © We can find ordinal value(index value) of enum constant by using ordinal() method. Example: public int ordinal(); Example: enum Beer { KE,KO,RC,FO; } class Test{ public static void main(String args{]){ Beer{] b=Beer.values(); for(Beer b1:b)//this is forEach loop. { System.out.printin(b14"......."+bLordinal()}; D:\Enum>java Test kt ‘Specialty of java enum: When compared with old languages enum java’s enum is more powerful because in addition to constants we can take normal variables, constructors, methods etc which may not possible in old languages. Inside enum we can declare main method and even we can invoke enum directly from the command prompt. Example: enum Fish{ GOLD,APOLO,STAR; public static void iaain(String ares{]){ System.out.printin("enum main() method called"); y aaa DURGA SOFT Core java NAGOOR BABU Output: D:\Enum>java Fish enum main() method called ‘¢ Inaddition to constants if we are taking any extra members like methods then the list of constants should be in the 1* line and should ends with semicolon. ‘© If we are taking any extra member then enum should contain at least one constant. Any way an empty enum is always valid. Example: — Herm X{ lic void methodOne ‘uB.c/ There semicolon mandatory. | ble voH#methodOnel pubic vel mete AG, ) Walia) (invalid) ee ‘enura X Jenum X : Cage : ee } | publicvoid methodone( + twats Wwatie) |} Gyatigy : Enum vs constructor: Enum can contain constructor. Every enum constant represents an object of that enum class which is static hence all enum constants will be created at the time of class loading automatically and hence constructor will be executed at the time of enum class loading for every enum constants. Example: ‘enum Beer{ KF,KO,RC,FO; Beer(){ System.out.printin("Constructor called."); } } class Test{ public static void main(String args{]}{ Beer b=Beer.KF; System.out.printin("hello.”"}; y Output: D:\Enum>java Test Constructor called. Constructor called. Constructor called. 445, DURGA SOFT 445 Core java NAGOOR BABU Constructor called. Hello. ‘* We can’t create enum object explicitly and hence we can’t invoke constructor directly. Example: enum Beer{ KE,KO,RC,FO; Beer(}{ System.out.printin("constructor called"); } } class Test{ public static void main(String args{}){ Beer b=new Beer(); ‘System.out.printin(b); y Output: Compile time error. D:\Enum>javac Test.java Test java:9: enum types may not be instantiated Beer b=new Beer(); Example: lkF==>public static final Beer KF=new Beer(); IkF(100}==>public static final Beer KF=new Beer(100} enum Beer { KF(100), KO(70),RC(65), Fo(90),KALYANI; int price; Beer(int price) this.price=price; } Beer() { this.pr } public int getPrice() { return price; I =125; a6 DURGA SOFT 446 Core java NAGOOR BABU } class Test{ public static void main(String args[}}{ Beer[] b=Beer.values(); for(Beer b1:b) { System.out.printin(b1+"......"+b1.getPrice()); m ‘* Inside enum we can take both instance and static methods but it is not possible to take abstract methods. Gsel: * Every enum constant represents an object hence whatever the methods we can apply on the normal objects we can apply the same methods on enum constants also. Which of the following expressions are valid? 1) Beer.KF==Beer.RC- 2). Beer.KF.equals(Beer.RC) 3) Beer.kF

You might also like