×
☰ See All Chapters

What is enum in java

An enum is a data type which contains fixed set of constants. It can be used for days of the week (SUNDAY, MONDAY, TUESDAY….), directions (NORTH, SOUTH, EAST, WEST) etc. The enum constants are static and final implicitly. It is available from java 5. An enum is a Java class that represents an enumeration.Enums can be thought of as classes that have fixed set of constants. Every enum in Java implicitly extends java.lang.Enumclass

Points to remember for Enum:

  1. enum may implement many interfaces but cannot extend any class because it internally extends Enum class. 

  2. You do not instantiate an enum. The constants defined in an enum are all implicitly public , final , and static , so there is no reason to create instances of the enum class. 

  3. enum can have fields, constructors and methods. These additional fields and methods must appear after the enum  list, and the enum list must end with a semicolon  in this situation. 

Because the elements of an enum are static, you can access them using the name of the enum. Behind the scenes, the compiler writes a class that extends Enum and creates an instance of the class for each element in the enum. This generated class contains a static field for each element in the enum.

An enumeration is created using the enum keyword. For example, here is a simple enumeration that lists various java technologies:

enum JavaTechnologies {

        SERVLET, JSP, JSF, JPA

}

 

The following code demonstrates the syntax for accessing enum elements.

enum JavaTechnologies {

        SERVLET, JSP, JSF, JPA

}

 

public class EnumDemo {

        public static void main(String args[]) {

                JavaTechnologies jt;

                jt = JavaTechnologies.JPA;

                // Output an enum value.

                System.out.println("Value of jt: " + jt);

                jt = JavaTechnologies.JSF;

                // Compare two enum values.

                if (jt == JavaTechnologies.JSF)

                        System.out.println("jt contains JSF.");

                // Use an enum to control a switch statement.

                switch (jt) {

                case SERVLET:

                        System.out.println("SERVLET");

                        break;

                case JSP:

                        System.out.println("JSP");

                        break;

                case JSF:

                        System.out.println("JSF");

                        break;

                case JPA:

                        System.out.println("JPA");

                        break;

                }

               

                //values() method

                JavaTechnologies allJavaTechnologies[] = JavaTechnologies.values();

                for(JavaTechnologies javaTechnologies : allJavaTechnologies) {

                        System.out.println(javaTechnologies);

                }

               

                //valueOf() method

                jt = JavaTechnologies.valueOf("JSP");

                System.out.println("jt contains " + jt);

 

        }

}

Output:

Value of jt: JPA

jt contains JSF.

JSF

 

The values( ) method returns an array that contains a list of the enumeration constants.

valueOf( ) method of enum accepts exactly same String which is used to declare Enum constant to return that Enum constant.valueOf method is case-sensitive and invalid String will result in IllegalArgumentException. In short String passed to valueOf() method of Java enum must be same as which is used to declare Enum constant.

Java Enumerations Are Class Types

Inside Enumerations we  can write constructors, add instance variables and methods, and even implement interfaces.But we cannot instantiate an enum using new because constructor of enum type is private if we don’t declare constructor as private the compiler will  automatically declares that as private.

But it is important to understand that each enumeration constant is an object/instance of its enumeration type. Thus if we write any parameterized constructor explicitly inside enum, then while creating enum constants we have to pass arguments to that constructor.

If we don’t write parameterized constructor the default constructor will be added ,in this case an enumeration can’t inherit another class and  enum cannot be a superclass.

enum JavaTechnologies {

        SERVLET("WebTechnology"), JSP("WebTechnology"), JSF("MVCFramework"), JPA("PersistanceTechnology");

 

        private String type; // price of each apple

 

        String getType() {

                return type;

        } // getter method

 

        JavaTechnologies(String type) {

                this.type = type;

        } // Constructor

}

 

public class EnumDemo {

        public static void main(String args[]) {

                // values() method

                JavaTechnologies allJavaTechnologies[] = JavaTechnologies.values();

                for (JavaTechnologies javaTechnologies : allJavaTechnologies) {

                        System.out.println("Technology: " + javaTechnologies+ ", Type: " + javaTechnologies.getType());

                }

        }

}

Output:

Technology: SERVLET, Type: WebTechnology

Technology: JSP, Type: WebTechnology

Technology: JSF, Type: MVCFramework

Technology: JPA, Type: PersistanceTechnology

 

Enumerations Inherit Enum

Although you can’t inherit a superclass when declaring an enum, all enumerations automatically inherit java.lang.Enum. This class defines several methods that are available for use by all enumerations.

The following program demonstrates the ordinal( ), compareTo( ), and equals( ) methods present in Enum class.

enum JavaTechnologies {

        SERVLET, JSP, JSF, JPA

}

 

public class EnumDemo {

        public static void main(String args[]) {

                JavaTechnologies jt;

                jt = JavaTechnologies.JSF;

                // compareTo

                if (jt.compareTo(JavaTechnologies.JSP) > 0) {

                        System.out.println("JSF comes after JSP.");

                }

                if (jt.compareTo(JavaTechnologies.JPA) < 0) {

                        System.out.println("JSF comes before JPA.");

                }

               

                if(jt.equals(JavaTechnologies.JSF)) {

                        System.out.println("jt contains JSF.");

                }

               

                JavaTechnologies allJavaTechnologies[] = JavaTechnologies.values();

                for (JavaTechnologies javaTechnologies : allJavaTechnologies) {

                        System.out.println("Ordinal:" + javaTechnologies.ordinal());

                }

 

        }

}

Output:

JSF comes after JSP.

JSF comes before JPA.

jt contains JSF.

Ordinal:0

Ordinal:1

Ordinal:2

Ordinal:3

 


All Chapters
Author