What is java enum(enumerators)

Learn Programming </>
2 min readDec 20, 2023

Hi😉

Today let’s talk about java enum and how it work 😎😉

To reading this article you can get basic idea about the enum so enough talking let’s dive into middle deep.😉

This article I hope to talk about below topic,

  1. What is enum
  2. Why use enum
  3. How to define enum in java
  4. Conclusion

WHAT IS ENUM

Simply java enum is special data type(user define data type) that consists of a set of pre-defined named values. Java enum was introduced in java 5, but still it is widely used even today.

WHY USE ENUM

Normally in java there are two types of data, which are:

  1. Inbuild data

2. User define data

Let’s assume there are some data of different data types required to be stored in a single variable. In such a situation, inbuilt data types won’t fulfill the need. That’s why there is a requirement for user-defined Data Types, and enum in Java is one of them. Enum values can access using a dot(.).

Also keep in mind that we don’t need to extend or implement enum class to access the values stored in an enum . We can directly create an object (also we can directly assign enum calss to varable like this ‘EnumClass= example.test’) to enum class and can access using dot(.) even with out using new keyword.

I ill provide to an example for get clear idea

TestEnum te = TestEnum.testValue 
TestEnum → This is the enum class
testValue → This is the value that we need to access .

HOW TO DEFINE ENUM IN JAVA

Below this topic we are goin to discuss how to define enum or how to use enum in practically, I will give to an example with some code snipts to understand about it.

Before we jump to this section , I think you guys are already know how to create enum class and java basics, if you dont please watch this video also you can get fully idea about enum.

enum Names{
ALEX,ELLI,DIANA; / / KEEP IN MIND WHEN DEFINING VALUES WE SHOULD NEED TO USE UPPERCASE
}

public classExample{

public static void main(String [] args) {
NamesCl= Names.ELLI; // HERE THE ACCESS VALUE
System.out.println(NamesCl);
}
}

Output = "ELLI"

In above code section help to get idea about how to define enum class and how to access it inside the main method. I hope this will help to get clear idea and your further works.

CONCLUTION

This article helps to get clear and better idea about the java enum,how it works and how to use it use in real time. This article also provide simple code example for your further works.

I have attached some links below to provide more information about enums,

  1. What is enum
  2. How to define enum using java

--

--