Enums
Anonymous contributor
Anonymous contributor3077 total contributions
Anonymous contributor
Published Aug 4, 2021Updated Jan 2, 2023
Contribute to Docs
Enumeration (enum) is a special user defined type where we specify a set of values for a variable and the variable can only take one out of a small set of possible values. Enumerations are classified as a reference data type.
The keyword enum
is used to define an enumeration.
An enum
can contain constants, methods, etc.
Syntax
Here’s the basic syntax for creating an enum
:
public enum name {constant1,constant2,...}
Here’s an enum
with compass directions:
public enum Direction {NORTH, SOUTH, EAST, WEST}
Direction.NORTH
will have a value of 0.Direction.SOUTH
will have a value of 1.Direction.EAST
will have a value of 2.Direction.WEST
will have a value of 3.
Example
public enum Day {SUN, MON, TUE, WED,THU, FRI, SAT}
Day.SUN
will have a value of 0.Day.MON
will have a value of 1.Day.TUE
will have a value of 2.Day.WED
will have a value of 3.Day.THU
will have a value of 4.Day.FRI
will have a value of 5.Day.SAT
will have a value of 6.
To access one of the values:
Day day = Day.MON;
All contributors
- Anonymous contributorAnonymous contributor3077 total contributions
- Anonymous contributorAnonymous contributor5 total contributions
- Victoria-DR22 total contributions
- christian.dinh2481 total contributions
- Anonymous contributor
- Anonymous contributor
- Victoria-DR
- christian.dinh
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.