The enum in java is a special type of data types like Class and Interface. A Java enum is used to define collections of constants. It is also known as enumeration java. By the use of an enum, we can define a group of constants with all possible values at compile time. We can change the number of constants later and it doesn’t fit all time. Enum was introduced in JDK 1.5.
For example, we can use of enum in java to store the days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY). Because we know all the possible values at compile time. As per the naming conventions of the enum, all constants should be in capital letters.
Here is the table content of the article we will cover this topic.
1. What is enum?
2. Important points about enum?
3. How to create an enum?
4. Java enum example?
5. How to use of enum?
6. How to create Java enum methods and java enum constructor?
7. How to use String in Java enum string value?
8. Methods of enum?
Important points about enum
1. It is special data that is used to define a set of constants and the value of the constant defined at compile time.
2. All the constants of the enum are by default static and final. So, we can’t change the value of the constant. As constants are static so they are accessible by enum name.
3. A enum can have methods, constructors, and fields.
4. Java enums extend the Enum class that exists in java.lang package. The Enum class is an abstract class that implements a Comparable interface and serialize interface.
5. A enum can implements interfaces but can’t extend the class because it already internally extending the Enum class.
6. A enum can have numeric or string data constants.
How to create an enum
To create an enum we have to use an enum keyword like class keyword. It is the way to in java define enum.
accessModifier, we can provide access modifiers according to to use like default, and public.
enum, it is a predefined keyword.
enumName, which is the name of the enum to declare.
constant, We can have any number of constants.
methods, We can have any number of methods.
Java enum example
public enum Week { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; }
Here you can see the enum
keyword that is used in place of class
or interface
. The enum keyword indicates to the compiler that this type of definition is an enum.
How to use of enum
Suppose we have an enum Week that contains some constants. You can access any constant by using the enum name because all constants are static. Here is the syntax to access the constant.
enumName.constantName
You can refer to the constants in the enum above like this:
Week constantFromEnum = Week.MONDAY;
enum Week { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; } public class EnumExample { public static void main(String arg[]) { // Getting a constant Week constantFromEnum = Week.MONDAY; System.out.println(constantFromEnum); } }
Output: MONDAY
How to create Java enum methods and java enum constructor
We can create methods and constructors in an enum and use those methods to operate the constants of Enum.
Let’s see an example with a constructor and method. Here we are creating constants with an int code. So, we must provide a constructor that can take a parameter of int type otherwise compiler shows an error. This constructor internally involving by the compiler to set the code in constructors. Here we are creating a method that will return the code of each constant.
enum Week { SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5), FRIDAY(6), SATURDAY(7); private final int code; // Compiler internally invokes it to assign value Week(int code) { this.code = code; } public int getCode() { return code; } } public class EnumExample { public static void main(String arg[]) { // Getting a constant Week constantFromEnum = Week.MONDAY; System.out.println(constantFromEnum); System.out.println(constantFromEnum.getCode()); } }
Output: MONDAY
2
How to use String in Java enum string value
We can use any type of value in enum constants. Let’s see the example with a string value.
enum Week { SUNDAY(1, "First day"), MONDAY(2, "Second day"), TUESDAY(3, "Third day"), WEDNESDAY(4, "Fourth day"), THURSDAY(5, "Fifth day"), FRIDAY(6, "Sixth day"), SATURDAY(7, "Seventh day"); private final int intCode; private final String stringCode; // Compiler internally invokes it to assign value Week(int intCode, String stringCode) { this.intCode = intCode; this.stringCode = stringCode; } public int getIntCode() { return intCode; } public String getStringCode() { return stringCode; } } public class EnumExample { public static void main(String arg[]) { // Getting a constant Week constantFromEnum = Week.MONDAY; System.out.println(constantFromEnum); System.out.println(constantFromEnum.getIntCode()); System.out.println(constantFromEnum.getStringCode()); } }
Output: MONDAY
2
Second day
Methods of enum
Java enum class provides some predefined methods that are used to operate the values of an enum. Here we will discuss the most common method of the enum class.
compareTo() method
The compareTo() method is used to compare the constants of an enum. It compares the constant based on ordinal values.
enum Week { SUNDAY(1, "First day"), MONDAY(2, "Second day"), TUESDAY(3, "Third day"), WEDNESDAY(4, "Fourth day"), THURSDAY(5, "Fifth day"), FRIDAY(6, "Sixth day"), SATURDAY(7, "Seventh day"); private final int intCode; private final String stringCode; // Compiler internally invokes it to assign value Week(int intCode, String stringCode) { this.intCode = intCode; this.stringCode = stringCode; } public int getIntCode() { return intCode; } public String getStringCode() { return stringCode; } } public class EnumExample { public static void main(String arg[]) { System.out.println(Week.SUNDAY.compareTo(Week.MONDAY)); } }
Output: -1
toString() method
The toString() method converts the name of an enum to a string.
enum Week { SUNDAY(1, "First day"), MONDAY(2, "Second day"), TUESDAY(3, "Third day"), WEDNESDAY(4, "Fourth day"), THURSDAY(5, "Fifth day"), FRIDAY(6, "Sixth day"), SATURDAY(7, "Seventh day"); private final int intCode; private final String stringCode; // Compiler internally invokes it to assign value Week(int intCode, String stringCode) { this.intCode = intCode; this.stringCode = stringCode; } public int getIntCode() { return intCode; } public String getStringCode() { return stringCode; } } public class EnumExample { public static void main(String arg[]) { System.out.println(Week.SUNDAY.toString()); } }
Output: SUNDAY
Java enum valueof() method
The valueOf() method is a static method in the enum class and it is used to obtain an instance of the enum class for a given String value.
enum Week { SUNDAY(1, "First day"), MONDAY(2, "Second day"), TUESDAY(3, "Third day"), WEDNESDAY(4, "Fourth day"), THURSDAY(5, "Fifth day"), FRIDAY(6, "Sixth day"), SATURDAY(7, "Seventh day"); private final int intCode; private final String stringCode; // Compiler internally invokes it to assign value Week(int intCode, String stringCode) { this.intCode = intCode; this.stringCode = stringCode; } public int getIntCode() { return intCode; } public String getStringCode() { return stringCode; } } public class EnumExample { public static void main(String arg[]) { Week week = Week.valueOf("SUNDAY"); System.out.println(week); } }
Output: SUNDAY
This is some good stuff. It took me a while to locate this blog but it was worth the time. I noticed this article was buried in yahoo and not the number one spot. This internet site has a ton of decent stuff and it does not deserve to be burried in the searches like that. By the way I am going to save this web site to my favorites.
Great article. I was checking continuously to this web site and Im really inspired! Very helpful information, especially the second sentences. I really need this kind of knowledge. I used to be seeking this kind of information for a period. Thankx and best wishes.