Enum in Java

Enum in Java

Like any other keyword in Java, Enum is a keyword which was introduced in Java with JDK 5 releases. Enum are mostly used to define constants or well known as fixed variables in Java. It has some other popular uses, which have been discussed here.

Before introduction of Enum, Java classes are used to define constants but it had some serious drawbacks. There are some other advantages and uses of Enum apart from just defining constants. Below are some standard features of Enum.

1. Enums are type-safe means Enum has its own namesapce, we can’t assign any other value other than specified in Enum constants.

public enum Country {
	USA,
	INDIA,
	PAKISTAN,
	UK,
	CANADA
}

2. Enums are like any other class in Java with some exceptions, so we can define constructors, methods and variable inside Enum.

public enum Country {
	USA("United States of America"),
	INDIA("India"),
	PAKISTAN("Pakistan"),
	UK("United Kingdom"),
	CANADA("Canada");

	private String name;
	private Country(String name) {
	        this.name = name;
	}
	public String getName() {
	        return name;
	}

	@Override
	public String toString() {
	        return name;
	}
}

3. We can’t create any object of Enum outside of it, as constructors in Enums are private. So, values of enum constants specify at the creation time only.

Country country = new Country(); //compilation error

4. Enum constants are implicitly static and final. So it can’t be changed once created.

Country.USA = Country.INDIA; //compilation error 

5. We can use enum constants in switch statement and if condition using “==” operator. Refer here for more details.

6. Enum can’t extends any other class or Enum. But, it can implements interface to support multiple inheritance.

public interface SampleInterface {
	public void printData();
}
public enum Country implements SampleInterface {
	@Override
	public void printData() {
		System.out.println("testData");
	}
} 

7. As Enum can implements interface, so it can override methods as well. For example-

public enum Country {
	USA("United States of America"),
	INDIA("India"),
	PAKISTAN("Pakistan"),
	UK("United Kingdom"),
	CANADA("Canada");

	private String name;
	private Country(String name) {
	        this.name = name;
	}
	public String getName() {
	        return name;
	}

	@Override
	public String toString() {
	        return name;
	}
}

8. Enum has valueOf(String str) method which converts String into Enum constant, which is very useful while working with Enum and String.

String str = "USA";
Country country = Country.valueOf(str);
System.out.println(country + " : " + country.getName());

9. Enum has values() method which returns all constant of the enum.

Country [] countries = Country.values();
for(Country country : countries) {
	System.out.println(country);
}

Output

USA : United States of America
INDIA : India
PAKISTAN : Pakistan
UK : United Kingdom
CANADA : Canada

10. Order of constants are maintained in enum.

Some more useful links for Enum in Java are : Enum example, constructor overloading in Enum, uses of Enum.

2 thoughts on “Enum in Java

  1. Hi Team,

    Please add PL/SQL part also for database users.

    It would be a great help for us.

    Hope team will look into this point.

  2. Hi Team,

    Very nice team work and very unique concept.

    Its really going to help many users to improve their skills.

    Great work done. Keep it up.

Leave a Reply

Your email address will not be published. Required fields are marked *