How to Create an Enum in Java


In this example we will show you how to create an enum with a group of constants in Java.

Source Code

package com.beginner.examples;

enum Animal { // enum
  DOG,
  PIG,
  CAT
}

public class EnumAnimal { 
  public static void main(String[] args) { 
    Animal e = Animal.CAT; 
    System.out.println(e); 
  } 
}

Output:

CAT
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments