https://github.com/sanchezih/enums-java
https://github.com/sanchezih/enums-java
enums java-21
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sanchezih/enums-java
- Owner: sanchezih
- Created: 2024-07-09T18:03:20.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-01T03:35:09.000Z (over 1 year ago)
- Last Synced: 2025-04-15T22:54:17.357Z (8 months ago)
- Topics: enums, java-21
- Language: Java
- Homepage:
- Size: 11.7 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Enum Types


An _enum type_ is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Because they are constants, the names of an enum type's fields are in uppercase letters.
In the Java programming language, you define an enum type by using the `enum` keyword. For example, you would specify a days-of-the-week enum type as:
```java
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
```
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.
Java programming language enum types are much more powerful than their counterparts in other languages. The `enum` declaration defines a _class_ (called an _enum type_). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static `values` method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from the `Planet` class example below iterates over all the planets in the solar system.
```java
for (Planet p : Planet.values()) {
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
```
In the following example, `Planet` is an enum type that represents the planets in the solar system. They are defined with constant mass and radius properties.
Each enum constant is declared with values for the mass and radius parameters. These values are passed to the constructor when the constant is created. Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon.
**Note:** The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.
In addition to its properties and constructor, `Planet` has methods that allow you to retrieve the surface gravity and weight of an object on each planet.
Based on: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html