Java Enum Example

Enum type is a special data type which holds different constants such as WHITE, BLACK, RED. The convention is that they should be named with upper case because, again, they are constants. In java, you define enum type by using the enum keyword.

java-featured-image

public enum Macronutrients {
   FATS, CARBOHYDRATES, PROTEIN
}

If you know all the possible constants for your program at compile time, then you should represent them in an enum type.

Java introduced the enum data type in 1.5.

Other programming languages such as C++ or even C, also have enum data type, however in Java, it is more powerful. For example, in C/C++, enum is just a list of integer values whereas in Java, it is a class itself which extends Enum and generally, is nicer to both read and write. On top of that, due to the fact of enum being a class, it also supplies different methods which allow iteration over the enum members.




In Java, you can also use the name() method which will give you the member value from the enum data type:

public class Main {
    public enum Macronutrients {
        FAT, CARBOHYDRATES, PROTEIN;
    }

    public static void main(String[] args) {
        Macronutrients m = Macronutrients.PROTEIN;

        System.out.println(m.name()); // PROTEIN
    }
}

Output

PROTEIN

Enums in Java also give us flexibility – they can be either declared inside a class or outside of it.

Enum declared outside class:

enum Macronutrients {
    FAT, CARBOHYDRATES, PROTEIN, NONE
}

public class Main {
    
    public static void main(String[] args) {
        Macronutrients m  = Macronutrients.NONE;
        System.out.println(m.name()); // PROTEIN
    }
}

Output

NONE

Enum declared inside class:

public class Main {
    public enum Macronutrients {
        FAT, CARBOHYDRATES, PROTEIN, NONE
    }
    public static void main(String[] args) {
        Macronutrients m  = Macronutrients.NONE;
        System.out.println(m.name()); // PROTEIN
    }
}

Output

NONE

Every enum is an object of type enum. Remember when I said that enum gives us flexibility? Well, it can also be passed as an argument to a switch statement.

Example using switch statement

enum Macronutrients 
{ 
    FAT, CARBOHYDRATES, PROTEIN, NONE
} 
  
public class Main 
{ 
    Macronutrients macro; 
  
    public Main(Macronutrients macro) 
    { 
        this.macro = macro; 
    } 
  
    public void whichMacro() 
    { 
        switch (macro) 
        { 
        case FAT: 
            System.out.println("You've chosen FAT. A bit unhealthy, if it is the bad type of it."); 
            break; 
        case CARBOHYDRATES: 
            System.out.println("You've chosen CARBOHYDRATES. Let's hope it's not sugars, right?"); 
            break; 
        case PROTEIN: 
            System.out.println("You've chosen PROTEIN. Smart decision."); 
            break; 
        default: 
            System.out.println("You have not chosen anything. You must be starving.."); 
            break; 
        } 
    } 

    public static void main(String[] args) 
    { 
        Macronutrients carbs = Macronutrients.CARBOHYDRATES;
        Macronutrients fats = Macronutrients.FAT;
        Macronutrients protein = Macronutrients.PROTEIN;
        Macronutrients nothing = Macronutrients.NONE;

        Main instance1 = new Main(carbs); 
        instance1.whichMacro(); 

        Main instance2 = new Main(fats); 
        instance2.whichMacro(); 

        Main instance3 = new Main(protein); 
        instance3.whichMacro(); 

        Main instance4 = new Main(nothing); 
        instance4.whichMacro(); 
    } 
}

Output

You've chosen CARBOHYDRATES. Let's hope it's not sugars, right?
You've chosen FAT. A bit unhealthy, if it is the bad type of it.
You've chosen PROTEIN. Smart decision.
You have not chosen anything. You must be starving..

Key thing to remember:

  • Since enums are public static final it means it can be accessed by using enum Name but also since it is final, we can’t create childs of the enum.

What exactly is the difference between classes and enums? That’s a question you might be having and would be perfectly valid!

Main differences between classes and enum

  • java.land.Enum is extended by Enums and provides it with human-readable .toString method, .name and .ordinal methods and more.
  • enums can be used in switch statements
  • enum constructor provides us with some additional supporting methods such as values(), valueOf(), etc. which prove to be really useful;

 

0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments