Java Enumerations || Enum || Java example

Java Enumerations :

When ever you need to use a constants in program we need to implement them using enum. Java Enumerations is a special class which contains the constant declarations we require in various scenarios.

For example when we want to know the result of a student in examination we require few constants like

  1. Pass
  2. Fail
  3. Withheld
  4. Debar

These are the common words used in almost all the result patterns so when we want to print a message as Pass, consider the scenario where we are having a 100 students in which we may mistakenly print pas, passs, or any other word which is related to Pass like success, congrats.

So when we pass success, congrats though the meaning is same but our java program won’t consider it as same.It requires appropriate word which is defined in it.

When we make use if enum there is no chance for any mistake to be happening in terms of spelling or synonym word usage.

 

Declaration of enum with instance variable :

enum Result {

    Pass("Result : Pass"),
    Fail("Result : Fail"),
    Withheld("Result : Withheld"),
    Debar("Result : Debar");

    private String result;

    Result(String result) {
        this.result = result;
    }

    public String getValue() {
        return result;
    }

}

 

public class Enum {

    public static void main(String args[]) {

        Result result = Result.Pass;

        System.out.println(result.getValue());
     } 
}

 

java enumerations

 

 

Declaration of enum:

Declare constants inside the class

enum Result {

    Pass,
    Fail,
    Withheld,
    Debar
}

 

enum with if condition :

Assign a value to be parsed

Result result = Result.Pass;

 

public static void main(String args[]) {


    Result result = Result.Pass;

    if (result == Result.Pass) {

        System.out.println("Result : Pass ");
    } else if (result == Result.Fail) {

        System.out.println("Result : Fail ");
    } else if (result == Result.Withheld) {

        System.out.println("Result : Withheld");
    } else if (result == Result.Debar) {

        System.out.println("Result : Debar");
    }
}

 

 

enum with switch case :

 

public static void main(String args[]) {


    Result result = Result.Pass;
    
    switch (result) {

        case Pass:

            System.out.println("Result : Pass ");
            break;
        case Fail:

            System.out.println("Result : Fail ");
            break;
        case Withheld:

            System.out.println("Result : Withheld");
            break;
        case Debar:

            System.out.println("Result : Debar");
            break;

    }
}

 

 

enum with for loop :

for (Result status : Result.values()) {
    System.out.println(status);
}

 

java enumerations

 

General usage enum:

public static void main(String args[]) {

    Result result = Result.Pass;

    System.out.println("Result : "result);

 }

 

java enumerations

 

 

 

 

Show Buttons
Hide Buttons
Read previous post:
Switch Case || Java example

Java switch case When we have multiple options and based on condition we need to choose one then we can...

Close