In this article we will see about Enums in Java, we will come to know what is Enum in Java?, How to declare a Enum and How to access Enum values.

Enum were introduced in Java5.0 to restrict the value of a variable to some predefined options, this can reduce bugs in applications. For example if you want to define color of a car and want it to restrict to either BLUE, WHITE or BLACK, you can do that using an enum in a class like:

public class Car {
	enum CarColor {
		BLUE, WHITE, BLACK
	}

	CarColor color;
}

class Implementation {
	public static void main(String args[]) {
		Car car = new Car();
		car.color = Car.CarColor.BLACK;
	}
}

In the code above we defined a class Car with a instance variable color, but we wanted to restrict the value of color to either ot the three (BLUE, WHITE, BLACK). So we defined a enum inside a class with predefined three color values and make color of type CarColor. Now the color instance variable will hold only one of the value defined in CarColor enum.

Use Enum inside a class in java


We just saw that how a enum can be used inside a class, here is the syntax to define a enum in a class:

public class Car {
	enum CarColor {
		BLUE, WHITE, BLACK
	}

	CarColor color;
}

If a enum is inside a class than we can access them like:

Car car = new Car();
car.color = Car.CarColor.BLACK;

Use Enum outside a class in java


We can have a enum inside a class or outside the body of a class, see the example below how to define a enum outside the class body:

enum CarColor {
	BLUE, WHITE, BLACK
}

Now if we want to use enum values that is outside the class, we have a different syntax. See the example:

public class Car {
	CarColor color;
}

enum CarColor {
	BLUE, WHITE, BLACK
}

class Implementation {
	public static void main(String args[]) {
		Car car = new Car();
		car.color = CarColor.BLACK;
	}
}

In java it?s optional to put a semicolon at the end of enum declaration, see below examples of two valid declarations:

// with semicolon - valid
enum CarColor {
	BLUE, WHITE, BLACK
};

// without semicolon - valid
enum CarColor {
	BLUE, WHITE, BLACK
}


Get all enum values in java


Every enum has a static velues() method in it that returns all enums values in the form of an array, see the example below:

package com.tbNext.byexample;

enum CarColor {
	BLUE, WHITE, BLACK
}

class Implementation {
	public static void main(String args[]) {

		// returns all the values if enum as an array
		CarColor carColor[] = CarColor.values();

		// iterating through the array
		for (CarColor c : carColor) {
			System.out.println(c);
		}
	}
}

Output: This will print following output:

BLUE
WHITE
BLACK


We have gone through a brief discussion on enums their declaration and accessing values in a class and outside a class. But enums can do more than that, we can have constructors, instance variables and methods in enums, see the example below:

enum CarPrice {
	// constant values
	EXPENSIVE(234), NORMAL(23), CHEAP(2);

	// constructor
	CarPrice(int price) {
		this.price = price;
	}

	// instance variable
	int price;

	// methods
	public int getPrice() {
		return price;
	}
}

Now lets see how to use these enum capabilities in a class, lets create a class with CarPrice type instance variable:

class Car {
	CarPrice cost;
}

class Implementation {
	public static void main(String args[]) {
		Car car = new Car();
		// assign a predefined price to a car object
		car.cost = CarPrice.EXPENSIVE;

		// print actual price of this car
		System.out.println(car.cost.price);
	}
}


In this way we can have predefined values to instance variables and can assign properties to those values as well.

Enums are like classes when it comes to memory allocation, and each value of a enum is like an instance of that enum.

In this article we cam to know about enums in Java, what are enums, how to declare enums and how to iterate through the values of an enum. In upcoming articles we will see more about java and other open source technologies.
  • By Techburps.com
  • Mar 24, 2015
  • Core Java