Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Java Enum or Enumeration are mainly used to define some set of named constants that fall under the same group. It is a special type of Java class.
If I were to give a real-world example, you can consider a set of animals, which can have different names but fall under the same category of animals (ex – horse, goat, cow, lion etc). Here different animals can be referred to as enumerators and the “animals” category as an enumerated type.
The main motive of having enums in JAVA is to be able to define our own data types. It was added in Java version 5 and above.
One simple enum example could be an enum that has different sizes under it. This could be useful if you are creating a web application for an e-commerce platform and in similar use cases.
public enum SIZE { LARGE, SMALL, MEDIUM }
The enum keyword has a special meaning here. It informs the compiler that similar to other classes or interfaces, we are defining java enum as well.
Now how do we use an enum, or refer to one of the values in it? Just create an Enum object with the required enum value.
Size size = Size.SMALL;
In the above case, the size variable is of type Size and has been assigned a small value. We can assign LARGE and MEDIUM similarly.
Few things to consider while declaring an enum in java :
Example:
enum Size { SMALL, MEDIUM, LARGE; } public class Test { public static void main(String[] args) { Size size = Size.MEDIUM; System.out.println(size); } } //output MEDIUM
public class Test { enum Size { SMALL, MEDIUM, LARGE; } public static void main(String[] args) { Size size = Size.MEDIUM; System.out.println(size); } } //output MEDIUM
.equals()
method.main()
method within an enum as we would do in a class.
enum Size { MEDIUM, SMALL, LARGE; // Driver method public static void main(String[] args) { Size size = Size.MEDIUM; System.out.println(size); } } //output MEDIUM
java.lang.Enum
. However it can implement interfaces if required.
[su_note note_color=”#ffffff” text_color=”#000000″]
values()
– Values method is used to get all the values of an enum.ordinal()
– This method is used to determine the index of a constant in an enumvalueOf()
– Given a string, it returns the constant equivalent of that string.[/su_note]
Example of user-defined method within an enum :
enum Size { SMALL, MEDIUM, LARGE; private Size() { System.out.println("Size constructor initialized : " + this.toString()); } public void sizeDetails() { System.out.println("Covers size for all age groups!"); } } public class Sizes { public static void main(String[] args) { Size size = Size.SMALL; System.out.println(size); size.sizeDetails(); } }
Output:
Size constructor initialized : SMALL Size constructor initialized : MEDIUM Size constructor initialized : LARGE Covers size for all age groups!
All the above examples used the constants as default values for enums but java enum also supports custom values. We will take a look at how to define such enums, with the example where:
enum Books { JAVA(“AUTHOR 1”), CPP(“AUTHOR 2”), PYTHON(“AUTHOR 3”); }
Few pre-requisite for having a custom enum with values are:
[su_note note_color=”#ffffff” text_color=”#000000″]
[/su_note]
Example :
public enum JavaWorkshop { JAVA("Author 1"), CPP("Author 2"), PYTHON("Author 3"); private String programmingLangAuthor; private JavaWorkshop(String s) { programmingLangAuthor = s; } public String getProgrammingLang() { return programmingLangAuthor; } }
Then using it in a class.
public class JavaWorkshopExample { public static void main(String[] args) { System.out.println("Get author of book 'JAVA': " + JavaWorkshop.JAVA.getProgrammingLang()); } }
Output:
Get author of book 'JAVA': Author 1
Enums are useful and can be used only when the use case satisfies certain criteria.
Enum in java is tightly immutable. They expose certain methods which can be used to get all the values of an enum. This array of values can be used to create a new enum with some additional values as a workaround.
Yes, a java enum can be null. When a field of type enum is created in a class but not initialized, by default it is null.
Because they enable reusability and hence better code readability. And them being immutable add an extra layer of reliability for the constants defined within code.
If you liked the tutorial, feel free to share it on social media. If you have any doubts or something is missing from the post, leave them in the comments below.