Definition of enumeration() in Java
Java Enumeration signifies a set of constants contained within a class that denotes and specifies the functionality of the declarations made before the program’s execution begins. In java, enumerations act as a final variable; once declared, they help explain the concept being introduced within the program’s set of statements and control flow. The Enumeration in java came into existence in full fledge after releasing versions of JDK 5. Java Enumeration in java appears almost like enumerations in other languages besides the fact that in java concept of classes and object orientation come into the picture.
Syntax:
enum (name of user defined datatype)
{
Values of user defined datatype
}
Example:
To illustrate the syntax more precisely
enum days
{
sun, mon, tues, wed, thurs, fri
}
}
Explanation:
It works so that any enumeration of java within a class is provided using a keyword with enum followed by the type of datatype for a user and then giving values of user-defined datatypes. The above example illustrates clearly where the enum is the keyword followed by the name of user-defined datatypes and the value of user-defined datatypes.
How does the Enumeration () Method work in java?
The enumeration method plays a very important role in java; it came into the picture mostly after the release of the 1.5 version of JDK. More than C and C++. Java enum plays an important role in the sense it can also add variables, methods, and constructors to it. A good characteristic of Enumeration is that it is customizable according to the user data types. The following steps and rules will highlight the working of Enumeration:
- Enum declaration in java should never be declared within a method; it can be declared inside or outside a class.
- The beginning or first line of enum should always start with a list of constants and then the declaration of other conditional statements or methods, variables, and constructors.
- To customize our enumerations, it is good to start and follow the naming convention that the name should start with all capital letters.
- Every enum should be mandatorily implemented using class.
- Every enumeration constant should represent an object of type enum.
- The switch statement takes care of all the arguments needed to pass to illustrate the enum type.
- Every enum constant implicitly works as a public static final, and since it Is mostly static, it can be accessed using enum Name.
- No child enum can be created if an enum is declared as final.
- An enum can consist of the main method inside enum; therefore, it can get directly called from the command prompt during compile time.
- Enum also deals with inheritance in a way that all enum extends java.lang enum class.
- Unlike other classes which can extend only one parent class in java, an enum cannot extend anything.
- Lang.enum class gets overridden with the toString() method, which gives enum a constant name.
- Many interfaces consisting of constant values can be implemented using enum.
- There are several more methods present inside java.lang.enum package such as values () method, ordial()method and valueof() method.
- Enum contains constructor and gets executed segregate for each enum during compilation.
- Enum constructors cannot get invoked directly and cannot create enum constructors.
- Enum contains both concrete and abstract methods. If an enum class contains an abstract method, then each enum class must implement it.
- Enum in java also includes some customized values with their string values according to the user-defined.
- Also, one important point should be kept in mind, like creating a parameterized constructor as they cannot be protected or public or fully qualified, which is not in favor of enum constant.
- Therefore, it is needed to create one getter method to get the value of enums.
Examples of enumeration() in java
Examples of Enumeration () in java are given below:
Example #1
A program where enum can be declared outside any class but not inside any method.
Code:
enum Calendar
{
jan, feb, mar;
}
public class Test
{
public static void main (String [] args)
{
Calendar c1 = Calendar.jan;
System.out.println(c1);
}
}
Output:
Example #2
A program where enum can be declared outside any class but not inside any method.
Code:
public class Test
{
enum Calender
{
jan, feb, march;
}
public static void main (String [] args)
{
Calender c1 = Calender.feb;
System.out.println(c1);
}
}
Output:
Example #3
Code:
Program to illustrate working on an enum in switch case.
import java.util.Scanner;
enum Color
{
Saffron, Green, Yellow, White,
Black, violet, Grey;
}
public class Test
{
Color color;
public Test (Color color)
{
this.color = color;
}
public void colorIsLike ()
{
switch (color)
{
case Saffron:
System.out.println("saffron color signifies peace.");
break;
case Green:
System.out.println("Green is an ecofriendly color.");
break;
case Yellow:
case White:
System.out.println("White is a soothing color.");
break;
default:
System.out.println("Grey is a neutral color with dull mood.");
break;
}
}
public static void main (String [] args)
{
String str = "Saffron";
Test t1 = new Test (Color.valueOf(str));
t1.colorIsLike();
}
}
Output:
Example #4
Program to illustrate that enums have both constructor and concrete methods.
Code:
enum Calender
{
jan, feb, mar;
private Calender ()
{
System.out.println("Constructor called for: " +
this.toString());
}
public void calenderInfo ()
{
System.out.println("Universal Calender");
}
}
public class Test
{
public static void main (String [] args)
{
Calender c1 = Calender.jan;
System.out.println(c1);
c1.calenderInfo();
}
}
Output:
Example #5
Program to illustrate working of value(), ordinal() and ValueOf().
Code:
enum Calender
{
jan, dec, mar;
}
public class Test
{
public static void main(String[] args)
{
Calender arr[] = Calender.values();
for (Calender cal : arr)
{
System.out.println(cal + " at index "
+ cal.ordinal());
}
System.out.println(Calender.valueOf("jan"));
}
}
Output:
Conclusion
A conclusion can be made with the fact that enums in java have created a simplified way for the programmers to define and provide a detailed set of constants in a structured manner within or outside the class. Unlike C, C++ java enums play a pivotal role in structure definition and declaration.
Recommended Articles
This is a guide to Enumeration () in java. Here we discuss the introduction and how the enumeration method works in java, along with different examples and code implementation. You may also have a look at the following articles to learn more –