JAVA Enum

Java enum is a class in java that has a group of variables that cannot be changed. We can also call them constants or final variables. To create an enum, we use the enum keyword. It is used for things which are fixed such as days of the week – Monday, Tuesday, and Wednesday etc. Or the direction – East, West, North and South. Or the colors such as green, blue, red, yellow etc.

Java enum constants are static and final with a fixed set of constants. In Java, the pro of defining an enum is that we can create inside of the class as well as outside depending on our wish. But this cannot be done in other languages. Moreover, an enum cannot create its own class but it can extend other classes. It can also implement other interfaces just as java enum inherits enum class, but cannot extend any class. Java enum can also be used in switch statements. We use an enum to represent numeric or textual data which gives us a small set of possible value as output. Enum implements serializable interfaces in java, hence it is also possible to override enum.

As we know java ensures type safety which means we cannot perform an operation in java unless it is fully valid for the object. Java enum ensures type safety as well.

Java enum Example

enum direction { 
    EAST, WEST, NORTH; 
} 
public class Test 
{
    public static void main(String[] args) { 
        direction d = direction.EAST; 
        System.out.println(d); 
    } 
}

The output of this program will be:

EAST

Inside the program, the first thing should be a list of final variables and then could be constructor or methods etc.

Now we will discuss an example to see if we can how we can use enum inside class?

Now we will discuss an example to see if we can how we can use enum inside class?

class Enum{
enum Direction { EAST, WEST, NORTH, SOUTH; 
}
public static void main(String[] args) {
Direction d=Direction.EAST;
System.out.println(d);
}
}

The output of the above program will be:

EAST

Talking about constructors of enum type, they can only be private. As a result, it will automatically create final variables for itself which are defined at the starting of the enum block of the program. We can always invoke an enum constructor ourselves.

ENUM CONSTANTS

Enum is a special data type with which we can enable pre defined variables for a constant in java. Their initial is always capital as they are constants. The enum constants can have a value which starts from 0,1,2,3 and so on. But we have to define fields and methods further if we want to initialise a specific value to them.

Summary

  • We should always write the initial of enum final variables in Capital letter.
  • We use enum keyword for creating an enum class.
  • Enum can be traversed.
  • We can use enum in switch statements.
  • Enum constant will always represent an object or type enum.
  • Main() method can always be put inside enum.
  • We can always iterate an enum variable.
  • We cannot change the value of enum once created.
  • Enum cannot create its own class.

Leave a comment

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