Declaring Enums

13 February 2012 By Nithya Vasudevan 398 views One Comment
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

An enum is created using ‘enum‘ keyword in Java. When we define an enum, we are implicitly extending java.lang.Enum. The compiler converts our enum into a class which extends java.lang.Enum. We can create an enum in a separate file or as a member of another class.


enum PizzaSize {

SMALL, MEDIUM and LARGE are the enumerated constants. By default each named constant of the enum are implicitly declared as public, static, final member of the defined enum type (i.e.) PizzaSize in this example. Also, their type is the same as the enum type. So the enum is somewhat equivalent to,

public static final PizzaSize SMALL;
public static final PizzaSize MEDIUM;
public static final PizzaSize LARGE;

Unlike in C/C++ where enums are integer constants, Java expands enums to be class types thereby allowing instance variables, constructors and methods which are discussed in later sections.

Tags: , , , ,

  • Krishnan