Enums in Java

Baran Aslankan
BAU Yazılım ve Bilişim Kulübü
3 min readNov 29, 2021

Hey guys, this is my first medium writing ever so i am a bit excited. I decided to post my studies on medium every day and this is going to be my first day. Let’s get to the subject.

Java enums.

First, what is an enum ?

Enum is shortening of Enumeration. Enum is a special type of class that is for constants in Java.They are used when a variable has a constant value and they make the code more readable.

For Example

public enum WeekDays {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}

You can create and enum by writing “enum” keyword and define the constants by writing them and separate them using coma and at the end of the constant list you should put a semicolon.

To create an enum in Eclipse

By clicking the enum option you can create an enum in Eclipse.

When and How ?

We assigned day variable as monday in enum and we can use this variable like this.

Assigning values to the enum declarations

You can assign values to the enum declarations and use those values in your code by creating a variable and a constructor.

For Example

As you can see we created a final variable and created our constructor, the reason i declared this variable final is that i don’t want it to change later on use. Also we are getting an error, because we need to define the parameters.

As you can see i declared the numbers from 1 to 7 to the days of the week.

Usage

As you can see we can get the variable outputs and use them.

That was all from me, thanks for reading, have fun and take care ;)

--

--