Fork me on GitHub

Enumeration

An enumeration specifies a list of constant values assigned to a type.

The SARL enumeration is not object-oriented unlike the enumeration in the Java programming language. It means that you cannot define methods nor attributes in the enumeration.

1. Define an Enumeration

For defining an enumeration, you could use the enum keyword. The following example defines the enumeration MyEnum with two constants:

enum MyEnum {
	CONSTANT_1,
	CONSTANT_2
}

2. Modifiers

Modifiers are used to modify declarations of types and type members. This section introduces the modifiers for the enumeration. The modifiers are usually written before the keyword for defining the enumeration.

The complete description of the modifiers’ semantic is available on this page.

2.1. Top Enumeration Modifiers

A top enumeration may be declared with one or more modifiers, which affect its runtime behavior:

Examples:

public enum TopEnumeration1 {
	CST1, CST2
}
package enum TopEnumeration2 {
	CST3, CST4
}

2.2. Nested Enumeration Modifiers

A nested interface may be declared with one or more modifiers, which affect its runtime behavior:

Terminology Nested enumerations are divided into two categories: static and non-static. Nested enumerations that are declared static are called static nested enumerations. Non-static nested enumerations are called inner enumerations.

Note The modifiers may differ from the previously described, depending on the enclosing type, e.g. agent.

class EnclosingClass {
	public enum NestedClass1 {
		CST1, CST2
	}
	protected enum NestedClass2 {
		CST3, CST4
	}
	package enum NestedClass3 {
		CST5, CST6
	}
	private enum NestedClass4 {
		CST7, CST8
	}
	static enum NestedClass5 {
		CST9, CST10
	}
}

3. References

This documentation is based on documentations from the Xtext and Xtend projects, and from the Java tutorials. Thank you to the contributors to these documents.

4. Version Specification

Copyright © 2014-2025 SARL.io, the Original Authors and Main Authors.

Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.

Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator docs.generator 0.15.1.