Enumeration Concept in java using Netbeans

Basic Enumeration Concepts in Java using NetBeans

Enum (Enumeration) is a set of variables defined as a constant data type, Every object of type Enum, can access the set of values ​​contained in the Enumeration. This concept is similar to Array, but Enum is a constant or fixed and type class,  Enumeration is one of the features found in the J2SE Library version 5 in Java We can define a variable of type Enum by using the keyword enum, the set of variables we define in block { } (curly braces). An object in an enum can access an attribute or method (Enum), then apply the values ​​and values ​​contained in it.

Related Articles:

In this tutorial we will learn to understand the concept of enumeration in java, maybe the use of this concept is rarely found in programs or articles that discuss java programming, here let’s look at it further.

Basic Enumeration Concepts in Java

In the following example program, we will practice how to use the enum concept in Java, in the program to be created, we need several supporting functions such as constructors, void methods, Instances / Objects, and switch-case statements. I hope you have studied it before.

Open your NetBeans IDE, then create a new project and a new class, for example, we will name the class “EnumerationConcept”. Take a look at the following source code:

package com.mycompany.javabasics;

 * @author Fawadkhan
public class EnumerationConcept {

    //Creating Enum Class and Variables 
    public enum Zodiac {
    // Creating Objects from Enums
    private Zodiac zodiac;

    // A Constructor with Enum Parameters
    public EnumerationConcept(Zodiac zodiac) {
        this.zodiac = zodiac;

    // Defining Values for Each Enum Variable
    void ZodiacSign() {
        // The Switch Method Is Used To Determine Which Option To Choose
        switch(zodiac) {
            case AQUARIUS:
                System.out.println("AQUARIUS: January 21 - February 19");
            case ARIES:
                System.out.println("ARIES: March 21-19 April ");
            case CAPRICON:
                System.out.println("CAPRICON: December 22 - January 20 ");
            case LIBRA:
                System.out.println("LIBRA: September 23-October 23 ");
            case LEO:
                System.out.println("LEO: July 23-August 23 ");
            case SAGITARIUS:
                System.out.println("SAGITTARIUS: November 23-21 December ");
            case TAURUS:
                System.out.println("TAURUS: April 21-20 May ");
            case VIRGO:
                System.out.println("VIRGO: August 24-September 22 ");

    public static void main(String[] args) {
        // Create Instances / Objects and Determine Which Variables / Options to Choose        
        EnumerationConcept data1 = new EnumerationConcept(Zodiac.ARIES);
        EnumerationConcept data2 = new EnumerationConcept(Zodiac.AQUARIUS);
        EnumerationConcept data3 = new EnumerationConcept(Zodiac.LEO);
        EnumerationConcept data4 = new EnumerationConcept(Zodiac.TAURUS);
        EnumerationConcept data5 = new EnumerationConcept(Zodiac.VIRGO);




First of all, we create an instance variable and constructor of the class. In the Enum class, we declare several variables with Zodiac names.

ZodiacSign() method is used to define the value, in each enum variable, by using the switch statement, we can determine which option will be selected for us to display or run in the main method.

After that, we call, by creating a new object from the EnumerationConcept class with the parameters in the form of options or enum variables that we will select and run.

Finally, we call the ZodiacSign() method of the new object.

Furthermore, in this second example program, we will create an enum class and then in it, there are several variables with values ​​that have been defined directly, different from the previous example.

Example: how to use for loop in Enumeration in java using NetBeans:

package com.mycompany.javabasics;

 * @author Fawadkhan
public class EnumerationConcept {

        // Enum Class with Defined Value Variables
    public enum ANIMAL{
        DOG("Woof woof"),
        // String Variable to Store Data from Variable Enum
        private String animal;
        // A Constructor of Enum with String Parameters
        ANIMAL(String animal){
            this.animal = animal;
        //Getter Method to Return Animal Value
        String getAnimal (){
            return animal;
    public static void main(String[] args){

   //Array Object To Count the Number of Valeu on ANIMALS
        ANIMAL[] AnimalArray = ANIMAL.values();
        // Looping and Printing all Values in Enum
        for(ANIMAL H : AnimalArray)




In the source code, we can define values ​​directly on variables in the enum class, by creating an instance variable, then creating a constructor and creating a Getter method, to return a value to the String data type of the enum variable.

ANIMAL.values() is used to display all the values ​​in the enum class, then we define it on an array object so that we can display all of its values, by looping for each method.

That’s all from me, hopefully, it will be useful.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button