Enum and all its features - 1

"Hi, Amigo!"

"Hi, Amigo!"

"I'm going to tell you about something very interesting. It's pretty simple, but that makes it even more interesting. It's called an enum. An enum is a type that defines the specific set of values that a variable can have. Let's look at an example right off:"

Definition of an enum class
public enum Direction
{
 UP,
 DOWN,
 LEFT,
 RIGHT,
}
Using an enum
Direction direction = Direction.LEFT;

if (direction == Direction.LEFT)
 direction = Direction.RIGHT;
else
 direction = Direction.DOWN;

"So we just list a set of values and that's it?"

"Yes, we declare an enum and list all its possible values inside, separated by a comma."

"It's also quite easy to use."

"Can you set it to null?"

"Yes, an enum is a regular class, or rather it's kind of like a class, similar to how interfaces are like classes."

"So wherever I can use classes, can I use enum?"

"Yes."

"Can I declare an enum inside a class?"

"Yes."

"And inherit an enum?"

"No, you can't inherit an enum and an enum can't inherit other classes."

"Why is that?"

"Because the Java compiler converts enums to approximately this:

public final class Direction extends Enum
{
 public static final Direction UP = new Direction();
 public static final Direction DOWN = new Direction();
 public static final Direction LEFT = new Direction();
 public static final Direction RIGHT = new Direction();

 private Direction() {} // Private constructor
}

"As you can see from this example:"

1) The Direction class absolutely inherits the Enum class, so it can't inherit anything else.

2) The Direction class is declared as final, so nothing else can inherit it.

3) The Direction class's variables are actually public static final Direction variables. This is evident in code that uses them:

Direction direction = Direction.LEFT;

4) The Direction class contains only one constructor and it is private. That means that Direction objects can only be created using code inside the class. Other than the declared objects, no objects can be created.

5) Direction variables can be assigned a reference to any of the existing Direction objects. They are all set within the enum. There are no other objects of this type, and there won't be any others in the future.

6) Direction objects can be compared using «==», which will just perform a simple reference comparison."

"I wouldn't say it's super clear, but it's much clearer after your example."

"Great. Then here's a little more information for you:"

1) Each Direction object has its own unique number. The first (UP) is 0, the second (DOWN) is 1, the third (LEFT) is 2, etc. You can get this number using the ordinal() method. "Look at the screen:

Direction direction = Direction.LEFT;
int index = direction.ordinal();
int index2 = Direction.RIGHT.ordinal();

2) Each enum has a values() method that returns an array of the enum's values.

int leftIndex = Direction.LEFT.ordinal();

Direction[] array = Direction.values();
Direction left = array[leftIndex];

"This means we can get the index of any enum element, and then get the element again using that index."

"We can also use an enum in a foreach loop:"

for (Direction direction : Direction.values())
{
 System.out.println(direction);
}
Screen output:
UP
DOWN
LEFT
RIGHT

"Does that mean that enum overrides the toString method? After all, it doesn't display anything like:"
«com.codegym.Direction@123fd4»?

"Yes. Moreover, each enum, and therefore Direction, can be converted to a String and vice versa."

Convert to a String:
String left = Direction.LEFT.toString(); // left == "LEFT";
Convert a String to an enum:
Direction direction = Direction.valueOf("LEFT");

"Oh, got it."

"What happens if you pass a String to the valueOf function that isn't in Direction? For example, «AMIGO»?"

"What do you think?"

"An exception?"

"Yep. An IllegalArgumentException."

"Well, that wraps up our introduction to the world of enums."