list of dots

User Manual    [Previous]   [Next]   

Enumeration Definition

The Umple language allows developers to define enumerations for either a single class, or to share between classes. An enumeration is a user-defined data type that is a set of constants.

As shown in the examples below, enumerations require the keyword "enum" followed by the name of the enumeration, and its values. If duplicate enumerations are detected, E095 Duplicate Enumerations will be raised. If enumerations outside of class bodies have the same name as the enumerations within class bodies, the enumerations within class bodies will be used in the generated code.

Model-level enumerations will be added to classes when they do not already have enumerations defined with the same name, and the enumeration is detected in attribute types, method return types, method parameter types, and/or event parameter types.

Example

// In this example, the "Status" enumeration
// is defined for the "Student" class

class Student {
  enum Status { FullTime, PartTime }
  Status status;
}
      

Load the above code into UmpleOnline

 

Example of Enumeration Shared by Two Different Classes

// In this example, the "Status" enumeration
// is shared by the "GradStudent"
// and "UndergradStudent" class

enum Status { FullTime, PartTime }

class GradStudent {
  Status status;
}

class UndergradStudent {
  Status status;
}
      

Load the above code into UmpleOnline

 

Example of Enumerations Defined for a Single Class or Shared between Different Classes

// In this example, the "Status" enumeration
// is shared by the "GradStudent"
// and "UndergradStudent" class
// The "Semester" enumeration is only defined
// for the "UndergradStudent" class

enum Status { FullTime, PartTime }

class GradStudent {
  Status status;
}

class UndergradStudent {
  enum Semester { Spring, Summer, Fall, Winter }
  Status status;
  Semester semester;
}
      

Load the above code into UmpleOnline

 

Example with Enumerations Defined Within and Outside of Class Bodies

// In this case, the "Month" enumeration
// defined in class A will be used in
// the generated code

namespace example;

enum Month {x,y,z}

class A{
  enum Month {o,p,q}
  Month m;
  Month p;
}
      

Load the above code into UmpleOnline

 

Syntax


// Enumerations can be declared outside the body of classes
// See user manual page EnumerationDefinition
enumerationDefinition : enum [name] { [enumValue](, [enumValue])* }