Enum Example in Java

An enum type is a special data type that enables for a variable to be a set of predefined constants.

You can define an enum type by using the enum keyword. For example:

public enum Direction {
    NORTH,
	EAST, 
	SOUTH, 
	WEST    //optionally can end with ";"
}

We can access the enum constant like this:

Direction direction = Direction.NORTH;

Enum declaration inside a class:

public class DirectionClass
 {
     enum Direction
     {
         EAST,
         WEST,
         NORTH,
         SOUTH
     }
 }

We can access a direction using DirectionClass.Direction.EAST.


enum constructors :
We can define our own constructors to initialize the state of enum types.
public enum Direction {
	// Enum types
	EAST(0), WEST(180), NORTH(90), SOUTH(270);

	// Constructor
	private Direction(final int value) {
		this.value = value;
	}

	// Internal state
	private int value;

	public int getValue() {
		return value;
	}
}


extending enums:
Enum types are final by design and Java enums extend the abstract class java.lang.Enum implicitly, so we can't extend another class from our enum type.

implements enums:
We can implement Interfaces with Enums in Java.
interface TestInterface {
	public String testMethod();
}

public enum Direction implements TestInterface{
	// Enum types
	EAST(0), WEST(180), NORTH(90), SOUTH(270);

	// Constructor
	private Direction(final int value) {
		this.value = value;
	}

	// Internal state
	private int value;

	public int getValue() {
		return value;
	}

	@Override
	public String testMethod() {
		return "testing"+value;
	}
}


Enum is basically a special class type, and can have constructor, methods and fields just like any other class.
We can print the direction by defining a abstract direction method, also we can overwrite toString method.
public enum Direction {
	// Enum types
	EAST(0) {
		@Override
		void printDirection() {
			System.out.println("Direction is EAST");
		}
	},
	WEST(180) {
		@Override
		void printDirection() {
			System.out.println("Direction is WEST");
		}
	},
	NORTH(90) {
		@Override
		void printDirection() {
			System.out.println("Direction is NORTH");
		}
	},
	SOUTH(270) {
		@Override
		void printDirection() {
			System.out.println("Direction is SOUTH");
		}
	};

	// Constructor
	private Direction(final int value) {
		this.value = value;
	}

	// Internal state
	private int value;

	public int getValue() {
		return value;
	}

	@Override
	public String toString() {
		switch (this) {
		case EAST:
			return "EAST - " + value;
		case WEST:
			return "WEST" + value;
		case NORTH:
			return "NORTH" + value;
		case SOUTH:
			return "SOUTH" + value;

		default:
			return null;

		}

	}

	abstract void printDirection();
}










Related Examples


Write a Comparable to compare students based upon their age?
Simple Array Sorting Program
Write a program to convert from decimal to binary?
Write a program to get list of all the files from a given directory?
Producer Consumer Example in Thread
Write a program to reverse a String?
Write a Comparator to compare students based upon their name and age?








Receive Latest Java Examples in Your Email: