Although each value corresponds to a distinct integer value, the enum hides this implementation so that you don’t inadvertently misuse the values, such as using them to perform arithmetic. After you create an enum, variables, method arguments, and return types can be declared of that type.
Unlike Java, the enum type itself has no constructor syntax.
To define an enum, use the enum keyword in your declaration and use curly braces to demarcate the list of possible values. For example, the following code creates an enum called Season:
public enum Season {WINTER, SPRING, SUMMER, FALL}
By creating the enum Season, you have also created a new data type called Season. You can use this new data type as you might any other data type. For example:
Season e = Season.WINTER; Season m(Integer x, Season e) { if (e == Season.SUMMER) return e; //... }
You can also define a class as an enum. Note that when you create an enum class you do not use the class keyword in the definition.
public enum MyEnumClass { X, Y }
You can use an enum in any place you can use another data type name. If you define a variable whose type is an enum, any object you assign to it must be an instance of that enum class.
Any webservice method can use enum types as part of their signature. When this occurs, the associated WSDL file includes definitions for the enum and its values, which can then be used by the API client.
Apex provides the following system-defined enums:
This enum corresponds to the API error code that is exposed in the WSDL document for all API operations. For example:
StatusCode.CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY StatusCode.INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY
The full list of status codes is available in the WSDL file for your organization. For more information about accessing the WSDL file for your organization, see “Downloading Salesforce WSDLs and Client Authentication Certificates” in the Salesforce online help.
This enum returns a list of XML tags used for parsing the result XML from a webservice method. For more information, see XmlStreamReader Class.
This enum is used with the system.debug method, to specify the log level for all debug calls. For more information, see System Class.
This enum is used by methods that perform mathematical operations to specify the rounding behavior for the operation, such as the Decimal divide method and the Double round method. For more information, see Rounding Mode.
This enum is returned by the field describe result getSoapType method. For more informations, see SOAPType Enum.
This enum is returned by the field describe result getType method. For more information, see DisplayType Enum.
This enum is used for parsing JSON content. For more information, see JSONToken Enum.
This enum specifies the severity of a Visualforce message. For more information, see ApexPages.Severity Enum.
This enum specifies the node type in a DOM document.
All enum values, including system enums, have common methods associated with them. For more information, see Enum Methods.
You cannot add user-defined methods to enum values.