Back to: Java Tutorials For Beginners and Professionals
The enumeration in Java with Examples
In this article, I am going to discuss Enumeration in Java with Examples. Please read our previous article, where we discussed Swings in Java applications with Examples.
The enumeration in Java:
An Enumeration may be a list of named constants. Java Enumeration appears almost like enumerations in other languages. In C++, enumerations are simple lists of named integer constants but in Java, an enumeration defines a category type.
An enumeration is made using the enum keyword. Once you’ve got defined an enumeration, you’ll create a variable of that type. However all enumerations define a category type, we can instantiate an enum using new. Instead, you declare and use an enumeration variable in much an equivalent way as you are doing one among the primitive types. An enumeration value also can be used to control a switch statement. Of Course, all of the case statements must use constants from an equivalent enum as that employed by the switch expression. Java compiler will create .class for each class or interface or enum available within the program.
Points to Remember:
- Internally enum is created as a final that extends a predefined abstract class called ENUM.
- All constants are public static final.
- Every constant holds an object of the corresponding enum.
- Java enum is very efficient compared to C Language enum because in C Language enum contains only constants but in java, an enum can contain constants along with other codes like constructors, methods, etc.
Methods in Enumeration in Java:
values():
It returns an array that contains a list of the enumeration constants.
Declaration : public static enum-type[] values(); Here, enum-type is the type of the enumeration
valueOf(String str):
It returns the enumeration constant whose value corresponds to the string passed in str.
Declaration : public static enum-type valueOf(String str); Here, enum-type is the type of enumeration constants.
Sample Program to demonstrate the use of values() and valuesOf() methods
enum Fruit { Apple, Mango, Pineapple, Banana, Orange } public class EnumDemo1 { public static void main (String[]args) { Fruit fr; System.out.println ("Here are all Fruits constants:"); //Use values() Fruit allfruits[] = Fruit.values (); for (Fruit f:allfruits) System.out.println (f); System.out.println (); //Use valueOf() fr = Fruit.valueOf ("Mango"); System.out.println ("fr contains: " + fr); } }
Output:
Rules for Enumeration
- An enum can be empty.
- An enum can contain only constants
- An enum can contain constants along with other codes.
- An enum cannot contain only other code without constants.
- Terminating constants by; is optional when there is no other code is not available otherwise of other code is available then terminating constants by; is mandatory.
- When we write other code-writing constants must be the first line.
- We can also write the main() method in the enum.
Example:
enum Day{ SUN, MON, TUE, WED, THU, FRI, SAT; public static void main(String args[]) { System.out.println("This is enum"); } }
8. enum types cannot be instantiated that is, the object cannot be created for enum.
Example : Day d = new Day(); //It is invalid
9. We can write a constructor inside the enum which can be accessed by creating a constant in the enum.
Example:
enum Day{ SUN, MON, TUE, WED, THU, FRI, SAT; Day() { System.out.println("This is constructor"); } }
10. We can write instance variables and instance methods in the enum.
Sample Program to demonstrate the use of an enum Constructor, instance variable, and method
enum Fruit { Apple (10), Mango (9), Pineapple (12), Banana (15), Orange (8); private int price; //Constructor Fruit (int p) { price = p; } int getPrice () { return price; } } public class EnumDemo2 { public static void main (String[]args) { Fruit fr; // Display price of Mango System.out.println ("Mango costs: " + Fruit.Mango.getPrice () + "cents. \n"); //Display all fruits and prices System.out.println ("All fruits prices:"); for (Fruit f:Fruit.values ()) System.out.println (f + " costs " + f.getPrice () + " cents. "); } }
Output:
Note: Up to java 1.4 version we can pass byte/short/char/int datatype values into switch case. But in 1.5 version onwards we can pass primitive datatypes byte/ short/ char/ int and its corresponding Wrapper Classes like Byte/ Short/ Character/ Integer. From Java 1.5 onwards we can also pass a String into the Switch case as an argument. In Java 1.7 we can also pass a String into the Switch case as an argument.
Sample Program Enumeration in Java
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT; } public class EnumDemo3 { public static void main (String[]args) { Day d = Day.valueOf ("TUE"); switch (d) { case SUN: System.out.println ("It is a Jolly Day"); break; case MON: System.out.println ("First Working Day"); break; case TUE: System.out.println ("Second Working Day"); break; case WED: System.out.println ("Third Working Day"); break; case THU: System.out.println ("Fourth Working Day"); break; case FRI: System.out.println ("Fifth Working Day"); break; case SAT: System.out.println ("Shopping Day"); break; } } }
Output: Second Working Day
Note:- An enum cannot extend a class or enum, because an enum is internally created as a final class that extends a predefined enum class. We cannot create a class that extends any enum because internally enum is created as a final class and we cannot extend a final class. An enum can implement any number of interfaces because internally enum is created as a final class and a class can extend a class and also implement any number of interfaces.
Here, in this article, I try to explain Enumeration in Java with Examples and I hope you enjoy this Enumeration in Java with Examples article.