Topics:

  1. Introduction
  2. Declaration and Accessing Enum
  3. Enum Vs Inheritance
  4. Enum Vs switch
  5. Enum values() method
  6. Ordinal value
  7. Specialty of java enum

Introduction

  1. It has introduced in 1.5 version.An enum is a group of named constants and enum can be used for defining user defined datatypes.

    Example:

    public enum Month{
    	JAN,FEB,MAR;
    
    }
    
  2. When compared with old languages enum, the java enum is more powerful because it allows constructors,instance members,static members etc. which may not be possible in old languages enum.
  3. By using java enum, we can define our own data types.
    Example:
    JAN,FEB,MAR are the values of Month data type, which is user defined data type.

Declaration and Accessing Enum

  1. we can declare enum either outside the class or with in the class but not inside method,Violation leads to Compile time error saying enum types must not be local.
    Example:1 Outside class

    enum X{
    
    }
    class Test{
    
    }
    

    Example:2 Inside class

    class Test{
      enum X{
    
      }
    }
    

    Example:3 Inside method

    class Test{
     public void m1(){
      enum X{
    
      }
     }
    }
    /*
    Compile time error:enum types must not be local
    */
    
  2. Every constant inside enum is implicitly public static and final by default. And we can access enum constants by using enum name.
    enum Color{
    	RED,BLUE,BLACK,GREEN
    }
    
    public class EnumDemo {
    
    	public static void main(String[] args) {
    	
    		Color color=Color.RED;
    		System.out.println(color);
    	}
    
    }
    /*
    Output:
    RED
    */
    

Enum Vs Inheritance

  1. Every Enum in java implicitly extends java.lang.Enum always.As every enum is already extending java.lang.enum there is no chance of extending any other enum i.e Enum does not participate in inheritance
  2. But Enum is allowed to implement any number of interaces

Example:1

enum X{
}
enum Y extends X{
}
/*
Compile time error:
Can’t inherit from final X
*/

Example:2

enum X extends java.lang.enum{
 }
/*
Compile time error:
Can’t inherit 
*/

Example:3

enum X{
 }
class Test extends X{

}
/*
Compile time error:
Can’t inherit from final X
*/

Note: Every enum implicitly final,hence we are not allowed to create child enum or child class.
Example:4

interface X{

}
enum Y implements X{

}

Note:java.lang.Enum is an abstract class and it is direct child class of Object class.it implements Comparable and Serializable interfaces.Hence every java enum is by default Serializable and Comparable

Enum Vs switch

We can use enumtype as an argument to switch statement.

Example:

enum Color{
	RED,BLUE,BLACK,GREEN
}


public class DemoEnum{

	public static void main(String[] args) {
	
		Color color=Color.RED;
		System.out.println("color:"+color);
		switch (color) {
		case BLUE:
			System.out.println("Color BLUE found");
			break;
		case BLACK:
			System.out.println("Color BLACK found");
			break;
		case GREEN:
			System.out.println("Color GREEN found");
			break;
		case RED:
			System.out.println("Color RED found");
			break;
		default:
			System.out.println("NO COLOR FOUND");
			break;
		}
	}

}
/*
Output:
color:RED
Color RED found
*/

Note:

  1. If we are passing enum constants as the switch argument, All the case labels should be valid enum constants otherwise we will get compiler time error.
  2. Until 1.4 version,the following are allowed argument to the switch statement.
    byte,short,char,int
  3. But from 1.5 version on wards,in addition to above the following types also allowed.
    (byte,short,char,int),Byte,Short,Character,Integer,short,enum

Enum values() method

Every enum implicitly contains values method to list all it’s constants.

Example:

enum Color{
    RED,BLUE,BLACK,GREEN
}
 
public class DemoEnum {
 
    public static void main(String[] args) {
        Color[] colors=Color.values();
        for(Color color:colors){
            System.out.println(color);
        }
  
    }
}
/*
Output:
RED
BLUE
BLACK
GREEN
*/

Ordinal value

  1. The position of enum constant is important and it is described with ordinalvalue(). we can find ordinal value of an enum constant by using following method.
  2. public int ordinal():Ordinal value is 0(Zero) based.

Example:

package com.java2learn.enums;

enum Color{
	RED,BLUE,BLACK,GREEN
}

public class EnumDemo {

	public static void main(String[] args) {
		Color[] colors=Color.values();
		for(Color color:colors){
			System.out.println(color+"----"+color.ordinal());
		}
	}
}
/*
Output:
RED----0
BLUE----1
BLACK----2
GREEN----3
*/

Specialty of java enum

A java enum can contain constructors, instance members, static members etc in addition to constants which may not be possible in the old languages enum.

Example:

package com.java2learn.enums;

enum Color{
	
	RED(100),BLUE(200),BLACK(300),GREEN;
	int range;
	
	private Color(int range) {
	this.range=range;
	System.out.println("Argument constructor");
	}
	
	private Color() {
		this.range=5000;
		System.out.println("No Argument constructor");
	}
	public int getRange(){
		return range;
	}
	
}

public class EnumDemo {

	public static void main(String[] args) {
		Color[] colors=Color.values();
		for(Color color:colors){
			System.out.println(color.getRange());
		}
		
	}

}

Output:
Argument constructor
Argument constructor
Argument constructor
No Argument constructor
100
200
300
5000

An enum can contain constructors also and these will execute at the time of enum class loading.Programmer is not responsible to call constructors explicitly. With in enum we can’t take abstract methods.

case:1

If the enum contains any thing other than constants(like instance variables, static variables.. etc) then the list of constants should end with semicolon.

enum Color{
RED(100),BLUE(200),BLACK(300),GREEN;//semicolon is mandatory. 
int range;
}

case:2

enum Color{
RED(100),BLUE(200),BLACK(300),GREEN//semicolon is optional. 

}

case:3
If the enum contains anything else other than constants then the list of constants should be the first line.

enum Color{
int range;
RED(100),BLUE(200),BLACK(300),GREEN;. 

}

Compile time error
case:4
enum in java internally implemented as class concept only. We are allowed to declare main() method in enum and we can involve directly from the command prompt.

Example:

package com.java2learn.enums;
public enum Month {

	JAN,FEB;
	public static void main(String[] args) {
		System.out.println("Enum main method");
	}
}

Output:
Enum main method