Last Updated:

enum in Java

enum in Java

Enum is an enumerable data type that stores one or a set of related constants. On the other hand, the enum type resembles an array of constants, but provides a large set of opportunities for both accessing and forming its constants.

To understand what enum is, you need to look at a few examples of how this type of data works. In addition, enum has its own methods for representing information in string or numeric format, and also allows you to create your own custom methods and fields in the enum description.

Syntax for creating a simple enumeration

 

A simple set of related constants is created as follows:

enum Countries{
Russia,
USA,
England,
France,
Japan

}

 

All constants are listed separated by commas, and the latter remains without a punctuation mark. In order to access any enumeration constant, you need to create a variable of type enum:

 

Contries country = Countries.Japan;
System.out.print(country); \\  variant of accessing the constant through the variable enum
System.out.print(Countries.Japan); \\  output value without initially saving to a variable

Like any other data type, enum can become instances of classes, stored in arrays, lists, and other structures. When you output to the console, the value of Russia was converted to a string automatically. However, this constant has a value of type int.

Since Russia is the first constant, its serial number is 0. By default, each constant has its own sequence number. However, each of them can be assigned a separate value. But to do this, you will have to describe enum almost as a class.

An example of creating an enumeration with values and constructors:

\\ Create an enum with cities, each of which provides the address of a branch

enumFilials_Cities {

privateStringadress; \\ Create a private helper field for the constructor and assigning each address constant

publicStringget_adress(){ \\ Method to get the address in each city
returnadress;

}

Filials_Cities(String set_adress){ Constructor for creating a city and address
this.adress = set_adress;

}

\\ Create enumeration constants through the constructor and assign a value to each of them.
London ("Parker-street 22"),
Moscow ("Parkhomenko street no. 22"),
Tokyo ("Otemati 25")

}

In the example above, we created a complex enum structure and added functionality to it. Now you can use it.

Example:

System.out.println(Filials_Cities.London); \\ will output London
System.out.println(Filials_Cities.London.get_adress()); \\ will output Parker-street 22

Definition of individual methods for individual constants

The value of a constant can be not only a string, but even mathematical expressions, objects, arrays, and other elements. For each constant, you can define individual methods that form the value of the constant depending on the parameters of these methods.

Example of defining individual methods:

\\ Set enum with individual mathematical methods
enumMath_op{

public abstract int operate(int a, int b);

ADD{
publicint operate(int a, int b){return a+b;}
}
SUBSTRACT{
publicint operate(int a, int b){return a-b;}
}
MULT{
publicint operate(int a, int b){return a*b;}
}
DIV{
publicint operate(int a, int b){return a+b;}
}
DIV_with_REMAINDER{
publicint operate(int a, int b){return a%b;}

}
}

As you can see, for each constant, a certain method with the same name was assigned, which performs different mathematical operations. Before you can assign a method, you must define it in the enum field as abstract for all constants.

This method is then overwritten for each constant. In our case, only the operators have changed. The method to be assigned must fully conform to the abstract signature—the same name, type, and number of parameters, the return type, or the void.

Now you can take advantage of the features of our Math_op:

System.out.println(Math_op.ADD.operate(5, 5)); \\ will output "10" as the result of the method
System.out.println(Math_op.SUBSTRACT.operate(5, 5)); \\ 0
System.out.println(Math_op.MULT.operate(5, 5)); \\ 25
System.out.println(Math_op.DIV.operate(5, 5)); \\ one
System.out.println(Math_op.DIV_with_REMAINDER.operate(5, 5)); \\ 0

As you can see from this example, the enumerated type has a similar structure to the class, only less flexible. In addition, enum objects have their own built-in methods that provide the result in a specific format. It is known that when we refer to a constant, we will get its name. An enumerated type is also an ordinal type, so instead of a constant name, you can get a sequence number.

Built-in methods for enum

 

The static values() method returns an array of all constants in the defined enum:

enum Numbers{
ONE,
TWO,
THREE,
FOUR,
FIVE

}


Numbers[] num_arr = Numbers.values();
for (Numbers n : num_arr) {
System.out.println(n);
} \\ ONE, TWO, THREE, FOUR, FIVE

The static ordinal() method returns the sequence number of the constant for which it applies:

System.out.println(Numbers.ONE.ordinal()); \\ 0
System.out.println(Numbers.TWO.ordinal()); \\ 1
System.out.println(Numbers.THREE.ordinal()); \\ 2
System.out.println(Numbers.FOUR.ordinal()); \\ 3

Features of constants

 

It is worth mentioning that when assigning values to constants through the constructor, their values can no longer be changed - this will lead to an error. You can change the value only if each constant is assigned individual methods. However, the results of these methods are not a constant value, but a separate result of the method execution.

The value of the constant is set through the constructor. If the constant does not have a value, that it is equal to the current sequence number of the constant, which can be determined through the ordinal() method.

Conclusion

 

Enum is a given primitive type that represents an array of constants. By default, the value of a constant is a sequence number. However, through their advanced functionality in Java, constant values can be changed and set through the constructor. The substitution value can be any data type provided in the language. This can be an array, a number, a string, an object, and a structure.

Enum is a primitive, so constants are stored in a stack rather than a heap. You cannot assign another enumeration to a variable of one type enum and this will result in an error. You cannot select any destination for an enumerated type.

This structure is used for its intended purpose. It is convenient to store constants in it that will not change - days of the week, months, dates and the like. It's safer for data because an array can always be accidentally broken in a program. Although you can also immerse constants in the array.