Enumerated Types (Java Enum)

Home arrow Java Tutorials arrow Java Essentials arrow Enumerated Types (Java Enum)
Enumerated Types (Java Enum) Print E-mail
Contributed by Joe   
Saturday, 01 July 2006

Enumerated Types (Java Enum) overview

There were two basic ways to define new types: through classes and interfaces. For most object-oriented programming, this would seem to be enough.The problem is that there are still some very specific cases . There has  a new enumerated type in jdk5.0 to solve this problem(generally referred to simply as an enum).

 

Enumerated Types (Enum) defination

An enumerated type is a type whose legal values consist of a fixed set of constants. Common examples include compass directions, which take the values North, South, East and West and days of the week, which take the values Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.

In the Java programming language, you define an enumerated type by using the enum keyword. For example, you would specify a days of the week enumerated type as:

enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,              FRIDAY, SATURDAY };

How to create an Enum

Creating an enumerated type involves three basic components, at a minimum:

  • The enum keyword
  • A name for the new type
  • A list of allowed values for the type

There are several optional components that may be defined as well:

  • An interface or set of interfaces that the enum implements
  • Variable definitions
  • Method definitions
  • Value-specific class bodies
 Creating a simple enumerated type

public enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
            FRIDAY, SATURDAY };

Referring to an enum in another class

public class Student {
private String firstName;
private String lastName;
private Grade grade;
public Student(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getFirstName( ) {
    return firstName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public String getLastName( ) {
    return lastName;
  }
  public String getFullName( ) {
    return new StringBuffer(firstName)
           .append(" ")
           .append(lastName)
           .toString( );
  }
  public void assignGrade(Grade grade) {
    this.grade = grade;
  }
  public Grade getGrade( ) {
    return grade;
  }

 Below code is actually using this code in conjunction with the enum.


         public void testGradeAssignment(PrintStream out) throws IOException {
           Student student1 = new Student("Brett", "McLaughlin");
           Student student2 = new Student("Ben", "Rochester");
           Student student3 = new Student("Dennis", "Erwin");
           student1.assignGrade(Grade.B);
           student2.assignGrade(Grade.INCOMPLETE);
           student3.assignGrade(Grade.A);
         }


Below are highlights about enums (java enums)
  • Enums are classes
  • Enums extend java.lang.Enum

      java.lang.Enum is a new class in Tiger, and is not itself an enumerated type. All enumerated types implicitly extend Enum.

  • Enums have no public constructor

This removes the ability to create additional instances of the enum not defined at compile-time. Only those instances defined by the enum are available.

  • Enum values are public, static, and final
  • Enum values can be compared with == or equals( )
  • Enums implements java.lang.Comparable

As a result, enum values can be compared with compareTo( ), and ordering occurs in the same order as values are declared in the enum declaration.

  • Enums override toString( )

The toString( ) method on an enumerated type returns the name of the value. Grade.INCOMPLETE.toString( ) returns the String "INCOMPLETE". However, this method isn't final, and can be overridden if desired.

  • Enums provide a valueOf( ) method

The static valueOf( ) method complements toString( ). Grade. valueOf("INCOMPLETE") returns Grade.INCOMPLETE.

Last Updated ( Saturday, 01 July 2006 )

  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground