Java Enums

We studied some types of Java. However, there is another type of Java called Enums. Enums, stand for enumerations i.e., a group of constants. Use enums to name constants.

We use the enum keyword to create an enum. Let us see the syntax:

Above, we have the enum name after the enum keyword. Then comes the enum items i.e., constants separated by comma.

Create an Enum

Let us now see how to create an enum. We will consider Directions and enum items as NORTH, SOUTH, etc:

Create an Enum Variable

Create a variable of the Enum if you want to access it. Let us create a variable of the above enum:

Assign a value to the enum variable

Access enum constants/ items with dot. Let us now assign value to an enum item and display:

Create an Enum inside a class

Let us see an example of creating an enum inside a class:

Output


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Java Regular Expressions
Java Methods
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading