Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
69 views
9 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
Download
Save
Save Enum For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
69 views
9 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
Carousel Previous
Carousel Next
Download
Save
Save Enum For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Enum For Later
You are on page 1
/ 9
Search
Fullscreen
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 440Core 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 444Core 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 442Core 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 443Core 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 SOFTCore 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 445Core 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 446Core 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
Enums
PDF
No ratings yet
Enums
14 pages
Java Enum
PDF
No ratings yet
Java Enum
5 pages
Enum
PDF
No ratings yet
Enum
14 pages
Enum in Java
PDF
No ratings yet
Enum in Java
13 pages
Enumeração JAVA em Ingles
PDF
No ratings yet
Enumeração JAVA em Ingles
7 pages
Advanced Java Programming M1
PDF
No ratings yet
Advanced Java Programming M1
12 pages
Enumerations, Autoboxing and Annotations-Module-01
PDF
No ratings yet
Enumerations, Autoboxing and Annotations-Module-01
76 pages
Modul 1 Advance Java & J2EE
PDF
No ratings yet
Modul 1 Advance Java & J2EE
30 pages
17CS553-AJJ - 5th-Sem-Modulewise-Notes-CSE - Muneshwara M.S
PDF
No ratings yet
17CS553-AJJ - 5th-Sem-Modulewise-Notes-CSE - Muneshwara M.S
132 pages
Enumaration PDF
PDF
No ratings yet
Enumaration PDF
6 pages
Enummm in Javaaa
PDF
No ratings yet
Enummm in Javaaa
14 pages
Enumeration Is A List of Named Constants. An Enumeration
PDF
No ratings yet
Enumeration Is A List of Named Constants. An Enumeration
9 pages
Enumeration Is A List of Named Constants. An Enumeration
PDF
No ratings yet
Enumeration Is A List of Named Constants. An Enumeration
9 pages
Enumeration Is A List of Named Constants. An Enumeration
PDF
No ratings yet
Enumeration Is A List of Named Constants. An Enumeration
9 pages
Enumeration Is A List of Named Constants. An Enumeration
PDF
No ratings yet
Enumeration Is A List of Named Constants. An Enumeration
9 pages
ENUM
PDF
No ratings yet
ENUM
8 pages
15CS553 - Advanced Java and J2EE - Module 1 PDF
PDF
No ratings yet
15CS553 - Advanced Java and J2EE - Module 1 PDF
24 pages
Advanced Java VTU Mod 1
PDF
No ratings yet
Advanced Java VTU Mod 1
24 pages
Java Enum
PDF
No ratings yet
Java Enum
6 pages
Enums in Java
PDF
No ratings yet
Enums in Java
5 pages
Java Programming Notes 7
PDF
No ratings yet
Java Programming Notes 7
21 pages
Enums: // Int Enum Pattern - Has Severe Problems!
PDF
No ratings yet
Enums: // Int Enum Pattern - Has Severe Problems!
7 pages
22 Enumeration
PDF
No ratings yet
22 Enumeration
17 pages
JMA Module 1
PDF
No ratings yet
JMA Module 1
29 pages
Module 1 Ajava
PDF
No ratings yet
Module 1 Ajava
20 pages
Java Enum
PDF
100% (1)
Java Enum
21 pages
Demystifying Java Enums: Let's Finally Understand Them
PDF
No ratings yet
Demystifying Java Enums: Let's Finally Understand Them
16 pages
17CS553 Notes 2019 20
PDF
No ratings yet
17CS553 Notes 2019 20
338 pages
Java Enums PDF
PDF
No ratings yet
Java Enums PDF
3 pages
Java Collection Framework
PDF
No ratings yet
Java Collection Framework
2 pages
Module 1 Notes
PDF
No ratings yet
Module 1 Notes
34 pages
Module5opp Part2
PDF
No ratings yet
Module5opp Part2
21 pages
Enum
PDF
No ratings yet
Enum
5 pages
Java F Mob M-1
PDF
No ratings yet
Java F Mob M-1
30 pages
Enums User Input
PDF
No ratings yet
Enums User Input
24 pages
2005 01 25 JDK5 Language New Features
PDF
No ratings yet
2005 01 25 JDK5 Language New Features
63 pages
1877677750
PDF
No ratings yet
1877677750
128 pages
Javaunit1 New
PDF
No ratings yet
Javaunit1 New
122 pages
Enumeration Autoboxing and Annotations
PDF
No ratings yet
Enumeration Autoboxing and Annotations
62 pages
M5 Enumerations
PDF
No ratings yet
M5 Enumerations
22 pages
Advanced Java J2EE Vtu Notes 17CS553
PDF
No ratings yet
Advanced Java J2EE Vtu Notes 17CS553
30 pages
Module-5 15CS561 Java
PDF
No ratings yet
Module-5 15CS561 Java
31 pages
Java - Enum Class
PDF
No ratings yet
Java - Enum Class
2 pages
Topics: - Static Variables and Methods (Revisit) - Enum Types - Java Pre-Defined Classes - String
PDF
No ratings yet
Topics: - Static Variables and Methods (Revisit) - Enum Types - Java Pre-Defined Classes - String
10 pages
2 Computer Programming Module 7
PDF
No ratings yet
2 Computer Programming Module 7
6 pages
Enums: Keerthana Technologies
PDF
No ratings yet
Enums: Keerthana Technologies
17 pages
Lesson17 PDF
PDF
No ratings yet
Lesson17 PDF
9 pages
Module 01
PDF
No ratings yet
Module 01
81 pages
1.what Is Enum? Explain The Use of Values and Valueof Methods? Enumerations
PDF
No ratings yet
1.what Is Enum? Explain The Use of Values and Valueof Methods? Enumerations
19 pages
Class 37
PDF
No ratings yet
Class 37
10 pages
Java 1
PDF
No ratings yet
Java 1
27 pages
Enum
PDF
No ratings yet
Enum
4 pages
Enums
PDF
No ratings yet
Enums
3 pages
ENUM - Make U R Names As Data Types
PDF
No ratings yet
ENUM - Make U R Names As Data Types
7 pages
Jhalak To Do This
PDF
No ratings yet
Jhalak To Do This
12 pages
Enums: Keerthana Technologies
PDF
No ratings yet
Enums: Keerthana Technologies
17 pages
Java Enumeration Notes
PDF
No ratings yet
Java Enumeration Notes
3 pages
Java Enums
PDF
No ratings yet
Java Enums
11 pages