In this course, we will learn the Enum Type in Java.
- What the Enum Type is
- How the Enum type is defined and used
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll talk about the Enum type. The Enum keyword was introduced in Java 5. Enum is a special type. It's a list of predefined constants. The main purpose of the Enum is to define our own data types. Generally, the field names of the Enum type are in uppercase letters as they are constants. The Enum keyword is used to create an Enum type. In our example, we define color Enum type, and color has some constant values, such as Yellow, Green, Blue, Orange, and Black. The test code Color myColor = Color.BLUE assigns a value to the variable myColor of the Enum type Color. The variable myColor can take any of the five defined values. In this case, it's set to blue. Java Enum type has some useful key points. Let's look at these key points. First, we can define a constructor in Java Enum. Enum constants are implicitly static and final. Enum can be used in switch case like string, int data type.
Equality operator and equals method are used to compare Enum constants. But we cannot create an instance of Enum by using the new operator. In other words, we cannot invoke an Enum constructor ourselves. Pay attention to this. Also, all Enum implicitly extend Java.lang.Enum because a class can only extend one parent. The Java language does not support multiple inheritances of state, and therefore, an Enum cannot extend anything else. Okay, now let's make some examples with Enum type to understand better. In this example, we'll write a program that displays some colors in Enum by getting input from the user. First, I'll create a new Java project. I click the file menu and select the New Java Project option.
The project name can be Enum type. The Java version will be Java SE1.8 and I click the finish button. Now, I'll create a new Enum in this project. In the Enum type project, right click on the source folder and this time, I select new Enum not the class. Specify the package name as Enum type and Enum name as colors. And click the finish button. Okay. In Enum colors, I'll define three contant colors: RED, YELLOW, and GREEN in uppercase letters. And save the code. Let's create the main test class. Right click on the Enum type package and select New Class. Specify the class name as ColorTest and select the checkbox to add the main method. So, we need to take inputs from the user by using the scanner class. Scanner scanner = new Scanner(System.in). Okay. We show a message to enter a color by using the print() method. S out ("Please enter a color"). Let's declare a variable input color with string type, String inputColor = and assign the next method of the scanner class to this variable. Now, I'll create an Enum instance named myColor and assign the value of method of Enum color to this variable. Colors myColor = Colors.valueOf.
We need to use the valueOf() method to convert the string to the Enum type. This valueOf() method takes a parameter that its name, inputColor. So in parentheses, I write inputColor, and I will change the input color text to uppercase letters by using the toUpperCase() method because we created the colors in the colors Enum in capital letters. Now, we use switch case statement in this part. The conditional switch expression is an instance of the Enum myColor. The first case value is RED. And we display, "You entered RED" in the print() method. And we add the break keyword. The second case value is YELLOW. And we display, "You entered YELLOW" in the print() method. And we add the break keyword again. Third case value is GREEN. And we display, "You entered GREEN" in the print() method. And we add break again. Okay, let's run the code. We enter red in lowercase letters. It's automatically converted to uppercase letters. A
nd "You entered RED" message is displayed. If we don't use the toUpperCase() method, we get an exception but we defined Enum constants as uppercase letters. Okay, let's run the code again. We enter GREEN in uppercase letters. "You entered GREEN" message is displayed. Okay, let's run the code again. We enter Java. We got an exception. The exception is the IllegalArgumentException. The exception message is, no Enum type constant in Enum color because Java is not defined in our Enum class. We defined only three Enum constants: RED, YELLOW, and GREEN. Let's handle this exception by using the try-catch block. I create a try-catch block here. And I'll move these codes in the try block.
In the catch block, I'll write llegalArgumentException with object e. And we display "Please enter Enum constant type [RED, YELLOW, GREEN]" in the print() method. Let's run the code again. We enter java in lowercase letters for color, and as you can see, the catch block handles the exception and our message is displayed in the console. Let's run the code again. We entered JAVA in uppercase letters this time. Also, the same message is displayed. In this program, if we enter except red, yellow, and green values, then the IllegalArgumentException is thrown. Yes, I think it's clear what the Enum type is. Let's take a short break here. We will develop a sample project in the next lesson. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.