Enum in JAVA

J Riyana
Nerd For Tech
Published in
6 min readAug 18, 2021

--

Java enum is a special type of class that we can use to represent constant variables.
Usually, we can write a constant value by using a final keyword.

package enumexamples;public class EnumExamples
{
public static void main(String[] args) {
final String ANIMAL = "dog";
System.out.println(ANIMAL);
}
}
//output -> dog

In case if you need to declare a constant variable outside of the main method, you have to declare it as a public and static variable. Because the main method is a public and static method. No other types can give the output.

package enumexamples;public class EnumExamples
{
public static final String ANIMAL = "dog";

public static void main(String[] args) {
System.out.println(ANIMAL);
}
}
//output -> dog

Assume that you have more than one constants to declare. Each time you have to declare as “public static final dataType VariableName”. So as a solution for this, java enum has been introduced.

Now let us see how we can declare, use enums and their features.

How to declare enum?

We can declare it as a class with a class name and constant variables.

enum animals{
DOG, CAT, RABBIT;
}

In the above example, the enum class name is animals and the constant variables are DOG, CAT, and RABBIT.

How to call constant variables that are inside an enum?

We can call a constant variable inside an enum class by using the enum class name and the variable name as shown in the below example. (enumClassName.constantVariable)

Method 1

package enumexamples;public class EnumExamples
{
enum animals{
DOG, CAT, RABBIT;
}

public static void main(String[] args) {
System.out.println(animals.DOG);
}
}
//output -> dog

Method 2

package enumexamples;enum animals{
DOG, CAT, RABBIT
}


public class EnumExamples
{
public static void main(String[] args) {
animals name = animals.RABBIT;
System.out.println(name);
}
}
//output -> RABBIT

Where you can use an enum?

1. Inside a class.

package enumexamples;public class EnumExamples
{
enum animals{
DOG, CAT, RABBIT;
}

public static void main(String[] args) {
System.out.println(animals.DOG);
}
}
//output -> dog

2. Outside of a class

package enumexamples;enum animals{
DOG, CAT, RABBIT;
}


public class EnumExamples
{
public static void main(String[] args) {
System.out.println(animals.DOG);
}
}
//output -> dog

3. As an external class in the same package.

For this let us take the enum class as animals and the Main class which calls the constant variable in the enum class.

Enum class - animals.java

package enumexamples;public enum animals{
DOG, CAT, RABBIT;
}

Main class - EnumExamples.java

package enumexamples;public class EnumExamples
{
public static void main(String[] args) {
System.out.println(animals.CAT);
}
}
//output -> CAT

Features of enums

1. Enum is a public and static class.

Because we call enums inside the main method. And only static and public variables are can call inside the main method.

2. Cannot create objects using enums.

package enumexamples;enum animals{
DOG, CAT, RABBIT
}

public class EnumExamples
{
public static void main(String[] args) {
animals obj = new animals();
obj = animals.CAT;

System.out.println(obj);
}
}
//output -> enum types may not be instantiated (error)

3. Can use enum in a switch case

We can call enum variables as usual and use them to the switch case as shown below.

package enumexamples;public class EnumExamples {

enum animals {
DOG, CAT, RABBIT;
}


public static void main(String[] args) {
animals name = animals.RABBIT;
switch(name) {
case DOG:
System.out.println("This is a dog");
break;
case CAT:
System.out.println("This is a cat");
break;
case RABBIT:
System.out.println("This is a rabbit");
break;

default:
System.out.println("Another animal");
}
}
}
output -> This is a rabbit

But there are two important features when using enums in switch cases.

i. We can only use the variables declared inside the enums as the case name. If we use any other it gives an error.

package enumexamples;public class EnumExamples {

enum animals {
DOG, CAT, RABBIT;
}

public static void main(String[] args) {
animals name = animals.RABBIT;
switch(name) {
case DOG:
System.out.println("This is a dog");
break;
case CAT:
System.out.println("This is a cat");
break;
case RABBIT:
System.out.println("This is a rabbit");
break;
case COW:
System.out.println("This is a cow");
break;

default:
System.out.println("Another animal");
}
}
}
//output -> an enum switch case label must be the unqualified name of an enumeration constant (error)

ii. We can only use enum variables to the switch variable. Any other variable that not declared inside the enum class are cannot use for the switch cases.

package enumexamples;public class EnumExamples {

enum animals {
DOG, CAT, RABBIT;
}

public static void main(String[] args) {
animals name = animals.COW;
switch(name) {
case DOG:
System.out.println("This is a dog");
break;
case CAT:
System.out.println("This is a cat");
break;
case RABBIT:
System.out.println("This is a rabbit");
break;
default:
System.out.println("Another animal");
}
}
}
//output -> cannot find symbol(error)

4. Enum class act as a normal class.

So that we can create constructors methods and variables inside the enum class.

enum class - animals.java

package enumexamples;
public enum animals {
//EnumVariableName("description", noOfLegs)
DOG("this is a dog", 4),
PARROT("this is a parrot",2),
CAT("this is a cat", 4),
RABBIT("this is a rabbit", 4),
SPIDER("this is a spider",8);

//Final variables
private final String DESC;
private final int NoOfLegs;

//constructor
animals(String description, int noOfLegs){
DESC = description;
NoOfLegs = noOfLegs;
}

//method to get description
public String getDesc(){
return DESC;
}

//method to get no. of legs
public int getNoOfLegs(){
return NoOfLegs;
}
}

Main class - EnumExamples.java

package enumexamples;public class EnumExamples {  
public static void main(String[] args) {
animals name = animals.CAT;
System.out.println("Animal name:" + name);
System.out.println("Description:" + name.getDesc());
System.out.println("No.Of legs:" + name.getNoOfLegs());
}
}
//output -> Animal name:CAT
Description:this is a cat
No.Of legs:4

5. Can call all values in enum by using a for each loop

For this, we can use a for-each loop. Inside the for each loop, we can declare an enum class variable and call all the details inside the enum class.

Main class - EnumExamples.java

package enumexamples;public class EnumExamples {  
public static void main(String[] args) {
for(animals name : animals.values()){
System.out.println(name + ", " + name.getDesc() + ", " +name.getNoOfLegs());
}

}
}

In the above example inside for each loop, we have the call values() method. This method is calls all the values inside the enum and stores them inside an array.

By using the same animals' enum class created in the 4th point if we run the above main class, the output will be as below. It shows all the details inside the enum class.

DOG, this is a dog, 4
PARROT, this is a parrot, 2
CAT, this is a cat, 4
RABBIT, this is a rabbit, 4
SPIDER, this is a spider, 8

We can modify this to print the output within a range. For this, we need to use java. util.EnumSet class. There it has a function as EnumSet.range. Inside it, we can give the range. By using this we can call values inside the enum within a range. r

package enumexamples;
import java.util.EnumSet;
public class EnumExamples {

public static void main(String[] args) {
for(animals name: EnumSet.range(animals.CAT, animals.SPIDER)){
System.out.println(name + " "+ name.getDesc()+" " + name.getNoOfLegs());
}
}
}
//output -> CAT this is a cat 4
RABBIT this is a rabbit 4
SPIDER this is a spider 8

Summary:

  • Enum is a special type of class.
  • Enum is used to declare constants.
  • Enum class is a public and static class.
  • We can create enum inside a class, outside a class, and as a new file in the same package.
  • Cannot create objects using enums.
  • For switch case can use only values declared in the enum.
  • Can create constructors, methods, variables inside the enum class.

♥️♥️ Thank you for reading. And stay tuned for upcoming articles. ♥️♥️

--

--

J Riyana
Nerd For Tech

I am an undergraduate in the Faculty of Information Technology University of Moratuwa, Sri Lanka.