Sign in

Java Enum fields & methods & constructors

An enum is a special “class” that represents a group of constants (unchangeable variables, like final variables).
To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. Note that they should be in uppercase letters:

Example:

enum Level {
LOW,
MEDIUM,
HIGH
}

You can access enum constants with the dot syntax:
Level myVar = Level.MEDIUM;

Enum Fields
You can add fields to a Java enum.
Thus, each constant enum value gets these fields.
The field values must be supplied to the constructor of the enum when defining the constants. Here’s an example:

public enum Level {
HIGH (3), //calls constructor with value 3
MEDIUM(2), //calls constructor with value 2
LOW (1) //calls constructor with value 1
; // semicolon needed when fields / methods follow


private final int levelCode;

private Level(int levelCode) {
this.levelCode = levelCode;
}
}

Notice how the Java enum in the example above has a constructor which takes an int. the enum constructor sets the int field. When the constant enum values are defined, an int value is passed to the enum constructor.
The enum constructor must be private. You can’t use public or protected constructors for a Java enum. If you do not specify an access modifier the enum constructor it will be implicitly private.

Enum Methods
You can add methods to a Java enum too. Here’s an example:

public enum Level {
HIGH (3), //calls constructor with value 3
MEDIUM(2), //calls constructor with value 2
LOW (1) //calls constructor with value 1
; // semicolon needed when fields / methods follow


private final int levelCode;

Level(int levelCode) {
this.levelCode = levelCode;
}

public int getLevelCode() {
return this.levelCode;
}

}

You call a Java enum method via a reference to one of the constant values.
Here’s Java enum method call example:

Level level = Level.High;
System.out.println(level.getLevelCode());

This code would print out the value 3 which is the value of the levelCode field for the enum constant HIGH.

You are not restricted to simple getter and setter methods. You can also create methods that make calculations based on the field values of the enum constant. if your fields are not declared final you can even modify the values of the fields (although that may not be so good an idea, considering that the enums are supposed to be constants).

If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum. Additionally, the list of enum constants must be terminated by a semicolon;

Overview
The enum keyword was introduced in Java 5. It denotes a special type of class that always extends the java.lang.Enum class. For the official documentation on their usage have a look at the documentation.
Constants defined this way make the code more readable, allow compile-time checking, document upfront the list of accepted values and avoid unexpected behavior due to invalid values being passed in.
Additionally, they come with many useful methods, which you would otherwise have to write yourself if you were using traditional public static final constants.

Adding a Constructor and a Final Field.
We’ll set the names into a final variable using a constructor:

public enum Element {
H("Hydrogen"),
HE("Helium"),
// ...
NE("Neon");
public final String label; private Element(String label) {
this.label = label;
}
}

First of all, we notice the special syntax in the declaration list.
This is how a constructor is invoked for enum types.
Although it’s illegal to use the new operator for an enum, we can pass constructor arguments in the declaration list.

we then declare an instance variable label. There are a few things to note about that.
First, we chose the label identifier instead of the name. Although the member field name is available to use, let’s choose label to avoid confusion with the predefined Enum.name() method.
Second, our label field is final. While fields of an enum do not have to be final, in most cases we don’t want our labels to change. In the spirit of enum values being constant, this makes sense.
Finally, the label field is public, so we can access the label directly:

System.out.println(BE.label);

On the other hand, the field can be private, accessed with a getLabel() method.
For the purpose of brevity, this article will continue to use the public field style.

References:
https://www.w3schools.com/java/java_enums.asp#:~:text=An%20enum%20is%20a%20special,the%20constants%20with%20a%20comma.

http://tutorials.jenkov.com/java/enums.html#enum-example