0% found this document useful (0 votes)
5 views11 pages

Java Enumerations Explained: Class Types

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

Java Enumerations Explained: Class Types

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java Enumerations

Understanding Java Enumerations as Class Types

H S Adithya Chakravarthy
4SH21CS046
Java Enumerations Are Class Types

 Java enumeration is a class type.


 Has similar capabilities to other classes.
 Can have constructors, instance variables, methods and
implement interfaces.
Enumeration Constants as Objects

 Each constant is an object of its enum type.


 Constructors called when constants are created.
 Each constant has its own copy of instance variables.
Example: Apple Enumeration
enum Apple {
Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15),
Cortland(8);
private int price;
Apple(int p) {
price = p;
}
int getPrice() {
return price;
}
Using the Apple Enumeration

Apple ap;
// Display price of Winesap.
[Link]("Winesap costs " +
[Link]() + " cents.\n");
Output:
Winesap costs 15 cents.
// Display all apple prices.
[Link]("All apple prices:");
for (Apple a : [Link]())
[Link](a + " costs " + [Link]() + " cents.");

Output:
Jonathan costs 10 cents.
GoldenDel costs 9 cents.
RedDel costs 12 cents.
Winesap costs 15 cents.
Cortland costs 8 cents.
Overloaded Constructors in Enums
enum Apple {
Jonathan(10), GoldenDel(9), RedDel, Winesap(15), Cortland(8);
private int price;
Apple(int p) {
price = p;
}
Apple() Output:
{
Jonathan costs 10 cents
price = -1; GoldenDel costs 9 cents
} RedDel costs -1 cents.
int getPrice() { return price; } Winesap costs 15 cents.
Cortland costs 8 cents.
}
•All enums inherit [Link].
Enumerations Inherit Enum
•Methods:
ordinal(), compareTo(),
equals().
•ordinal() :returns constant's
position.
•compareTo() :compares ordinal
values.
•equals() :checks if constants are
the same.
Using Enum methods
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo4 {
public static void main(String args[]) {
Apple ap, ap2, ap3;

// Display all ordinal values


[Link]("Here are all apple constants and their ordinal
values:");
for (Apple a : [Link]())
[Link](a + " " + [Link]());
ap = [Link];
ap2 = [Link]; OUTPUT:
ap3 = [Link];
[Link](); Here are all apple constants and their ordinal
// Demonstrate compareTo() values:
if ([Link](ap2) < 0) Jonathan 0
GoldenDel 1
[Link](ap + " comes before " + ap2);
RedDel 2
if ([Link](ap2) > 0)
Winesap 3
[Link](ap2 + " comes before " + ap);
Cortland 4
if ([Link](ap3) == 0)
[Link](ap + " equals " + ap3); GoldenDel comes before RedDel
[Link](); RedDel equals RedDel
//Demonstrate equals()
if ([Link](ap2)) RedDel equals RedDel
[Link]("Error!"); RedDel == RedDel
if ([Link](ap3))
[Link](ap + " equals " + ap3);
if (ap == ap3)
[Link](ap + " == " + ap3);
}
}
Thank
You…

You might also like