Enum Advanced Features

14 February 2012 By Nithya Vasudevan 1,224 views One Comment
2 Flares Twitter 0 Facebook 0 Google+ 2 2 Flares ×

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration is converted into a class which inherits java.lang.Enum. Since internally an enum is nothing but a class, it can include constructors, methods and other fields. The compiler automatically adds some special methods – values() and valueOf()method – during this conversion process.

Consider the same PizzaSize enum example. We have three constants SMALL, MEDIUM and LARGE. Let us say we would like to add the radius and thickness for each of the sizes. For that, we will create two fields of type double as follows.

private final double radius;
private final double thickness;

While defining the constants, the values for these fields are also passed which will be initialized by the constructor.

SMALL(6.0, 0.5), MEDIUM(9.0, 1.0), LARGE(12.0, 1.5);

We now create a constructor to initialize the fields as follows.

private PizzaSize(double radius, double thickness) {
	this.radius = radius;
	this.thickness = thickness;
}

The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. We cannot invoke an enum constructor ourselves.

We also create getter methods for radius and thickness and a method to calculate the volume.

public double getRadius() {
	return radius;
}

public double getThickness() {
	return thickness;
}

public double volume() {
	return Math.PI * radius * radius * thickness;
}

All these methods can be invoked on the enum variable just like any other methods.

The complete code is as follows.

package com.ibytecode.enums;

import java.text.DecimalFormat;
import java.util.Scanner;

public class PizzaEnumAdvanced {

	enum PizzaSize {
		SMALL(6.0, 0.5), MEDIUM(9.0, 1.0), LARGE(12.0, 1.5);

		private final double radius;
		private final double thickness;

		private PizzaSize(double radius, double thickness) {
			this.radius = radius;
			this.thickness = thickness;
		}

		public double getRadius() {
			return radius;
		}

		public double getThickness() {
			return thickness;
		}

		public double volume() {
			return Math.PI * radius * radius * thickness;
		}
	};

	PizzaSize size;

	public static void main(String[] args) {
		DecimalFormat df = new DecimalFormat("#.##");
		PizzaEnumAdvanced pizza = new PizzaEnumAdvanced();
		System.out.print("Enter pizza size (small, medium, large): ");
		Scanner s = new Scanner(System.in);
		pizza.size = PizzaSize.valueOf(s.next().toUpperCase());
		System.out.println("You selected " + pizza.size + " Pizza");
		System.out.println("Radius " + pizza.size.getRadius() + 
						" inches");
		System.out.println("Thickness " + pizza.size.getThickness() + 
						" inches");
		System.out.println("Volume " + df.format(pizza.size.volume()) 
					+ " cu. inches");
	}
}

Enter pizza size (small, medium, large): medium
You selected MEDIUM Pizza
Radius 9.0 inches
Thickness 1.0 inches
Volume 254.47 cu. inches

Tags: , , ,

  • Fábio A. Gomes

    muito legal esse mini tutorial sobre enums, very cool, congratulations : )